1.创建一个存储过程pud,将指定订单号和器件号的数量修改为指定的数量,然后调用该存储过程将订单号为“OR-01C”和器件号为“D1101”的数量改为10。
2.在数据库ordermanagement中创建存储过程khjb,查询指定年份和客户号的客户级别:订单总数量>=6的为“A级客户”, 订单总数量<6且>=4的为“B级客户”, 订单总数量<4的为 “C级客户”。然后调用该存储过程求客户号为“C10001”的客户在2001年的客户级别。
3.在数据库ordermanagement中创建触发器tgkh,当修改客户表中的某个客户号时订单表中同一客户号也自动改为一样的。然后测试该触发器是否正确。
4.在数据库stuinfo中创建after触发器txkrs,要求每位学生的选课不能超过5门。然后测试该触发器是否正确。
--问题一
create procedure pud(@订单 char(6), @器件 char(5), @newnum int)
as
update 订单明细表 set 数量 = @newnum
where @订单 = 订单号 and @器件 = 器件号
select * from 订单明细表
where @订单 = 订单号 and @器件 = 器件号
execute pud 'OR-01C', 'D11010', 10
--问题二
create procedure khjb(@年份 int, @客户 char(6), @level char(2) output)
as
declare @tmp int
set @tmp = (select count(*) from 订单表
where @年份 = year(订购日期) and @客户 = 客户号)
if @tmp >= 6 set @level = 'A'
else if @tmp >= 4 set @level = 'B'
else set @level = 'C'
declare @level char(2)
execute khjb 2001, 'C10001', @level output
select @level 客户级别
--问题三
--创建
create trigger tgkh
on 客户表
for update
as
if update(客户号)
begin
update 订单表
set 客户号 = (select 客户号 from inserted)
where 订单表.客户号 = (select 客户号 from deleted)
end
--测试
select * from 客户表 where 客户号 = 'A00112'
update 客户表 set 客户号 = 'A00113' where 客户号 = 'A00112'
select * from 客户表 where 客户号 = 'A00113'
select * from 订单表 where 客户号 = 'A00113'
--问题四
--创建
create trigger txkrs
on score
after insert
as
if exists (select count(cno) from score
where sno in(select sno from inserted)
having count(score.cno) > 3)
begin
raiserror('选课不能超过3门', 16, 2)
rollback
end
--测试
select * from score where sno = '111'
insert into score values('111', '3-245', 78)