- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
存储过程与触发器课堂练习及答案
创建一个存储过程,显示所有价格在 15 美元以下的书的书名,类型,价格。CREATE PROCEDURE show_title
AS
SELECT title,type,price FROM titles
WHERE price 15 GO
EXEC show_title
把价格作为参数,创建一个能显示在某两个指定价格之间的书的书名,类型,价格。CREATE PROCEDURE show_title2
@price1 money,@price2 money AS
SELECT title,type,price FROM titles
WHERE price between @price1 and @price2 GO
show_title2 12,20
使用 OUTPU 参数,创建一个计算圆柱体体积的存储过程。并执行它。CREATE PROCEDURE comp_area
@r smallint, @h smallint,
@result decimal(10,2) OUTPUT AS
SET @result = PI()*SQUARE(@r)* @h GO
DECLARE @answer decimal(10,2)
EXECUTE comp_area 2,3, @answer OUTPUT SELECT The result is: , @answer
A) 建立 price_change 表,准备用来存放书的价格变化信息,有以下几列: title_id, type, old_price, new_price, change_date, operat。or
B) 建立一个更新触发器,一旦 title表s create table price_change
发生更新,立即把相关信息存放到 price_change 表中。
(
title_id varchar(20), type varchar(20), old_price money, new_price money, change_date datetime,
operator varchar(20))go
create trigger tri_price on titles for update as
insert into price_change select ,,,,getdate(),user_name() from deleted o JOIN inserted n ON = go update titles set price = price*
修改练习 4,使得只有当 price列被更新时,才会触发触发器。create trigger tri_price
on titles for update as
if update(price) begin
insert into price_change
select ,,,,getdate(),user_name() from deleted o JOIN inserted n ON =
end go
创建一个存放书的编号、书名、类型、价格、对应作者的编号、姓名、电话、住址的视 图。use pubs
go
create view v_titledetail as
select , title, type, price, , au_lname, au_fname, phone, address
from titles t,titleauthor ta,authors a where = and =
为这个视图创建一个 Instead of更新触发器, 把对视图的更新放到触发器里面来做。 ( 假 设,
我们只允许更新这个视图的某几个列: price, phone, addres)s on v_titledetail
instead of update as
create trigger tri_titledetail
declare @price money, @phone varchar(20), @address varchar(40), @title_id varchar(20), @au_id varchar(20)
select @title_id = title_id, @price = price, @au_id = au_id, @phone= phone ,@address = address from inserted
update titles set price = @price where title_id = @title_id
update authors set phone = @phone, address = @address where au_
原创力文档


文档评论(0)