求助MSQL触发器问题

来源:百度知道 编辑:UC知道 时间:2024/06/08 02:54:01
在图书借阅系统中建了两个表:
图书信息表(图书编号,图书名称,可借天数)
图书借阅表(图书编号,读者证号,借出日期,应还日期)
其中"借出日期" default(convert(char(10),getdate(),120))
当向"图书借阅表"中插入记录时,如何自动生成"应还日期"
("应还日期"="可借天数"+"借出日期")
谢谢

CREATE TRIGGER triggername ON 图书借阅表
FOR insert
AS

begin

declare @bookid int ---图书编号
declare @canday int ----可借天数

select @bookid =图书编号 from inserted
select @canday=可借天数 from 图书信息表 where 图书编号=@bookid

begin tran
--
update 图书借阅表 set f_应还日期=借出日期+@canday where 图书编号=@bookid

if @@error<>0

begin
rollback tran
end

commit tran

end