- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
ex12触发器答案概要1
实验十二 触发器
请完成下面实验内容:
创建触发器,该触发器仅允许“dbo”用户可以删除Employee表内数据。
create trigger EmploteeDelete
on employee
for delete
as
if exists (select * from deleted)
begin
if user!=dbo
begin
print 该用户无权删除!
rollback
end
end
测试:(以sa用户)
delete from employee
where employeeno=E2005001
测试:(以其他用户)
sp_addlogin star,star$sjj0482
sp_adduser star,star
GRANT select,insert,delete,update
ON employee
TO star
delete from employee
where employeeno=E2006001
(2) 创建触发器,当向订单明细表添加销售明细数据时,统计该订单销售金额。要求:如果订单金额5000元及以上,则该订单中销售的所有商品按9折进行优惠处理(更新订单明细表中成交价格),同时还应更新订单总表中该订单的订单金额。
create trigger orderdetailIns
on orderdetail
for insert
as
begin
declare @orderno char(12),@sum int
declare mycur cursor for
select distinct orderNo
from inserted
open mycur
fetch mycur into @orderno
while(@@fetch_status=0)
begin
select @sum=sum(quantity*price)
from orderdetail
where orderNo=@orderno
if @sum=5000
begin
update orderdetail
set price=price*0.9
where orderNo=@orderno
update ordermaster
set ordersum=@sum*0.9
where orderNo=@orderno
end
fetch mycur into @orderno
end
close mycur
deallocate mycur
end
测试:
insert into orderdetail values(200801090001,1,900.00)
select a.*,b.*,c.orderno,c.ordersum
from product a,orderdetail b,ordermaster c
where a.productno=b.productno and b.orderno=c.orderno and c.orderno=200801090001
select b.orderno 订单编号,ordersum 订单金额,Price 成交价格,productPrice 商品定价
from product a,ordermaster b,orderdetail c
where a.productno=c.productno and b.orderno=c.orderno and c.orderno=200801090001
(3) 创建触发器,要求当修改Employee表中员工的出生日期或雇佣日期时,必须保证出生日期在雇佣日期之前,且雇佣日期与出生日期之间必须间隔周年及以上。
create trigger EmploteeUpdate on employee
for update
as
declare @birthday datetime,@date_hired datetime
if (update(birthday) or update(hiredate))
begin
declare getCur cursor for
select birthday,hiredate
from inserted
open getCur
fetch getC
原创力文档


文档评论(0)