我写的sql,如果读者信息表中状态不是正常,就不能在借阅表中添加记录 CREATE TRIGGER before_insert_jieyue ON [dbo].[借阅信息表] for INSERT,update ASBEGIN IF (select 状态 from dbo.读者信息表 where 读者编号=(select 读者编号 from inserted)) not in('正常') PRINT '账号不正常' ROLLBACK END 请大神看看哪错了 [最优解释]
CREATE TRIGGER before_insert_jieyue ON [dbo].[借阅信息表] FOR INSERT, UPDATE AS BEGIN IF EXISTS ( SELECT 1 FROM dbo.读者信息表 WHERE 读者编号 IN ( SELECT 读者编号 FROM inserted ) AND 状态 NOT IN ( '正常' ) ) PRINT '账号不正常' ROLLBACK END
[其他解释] CREATE TRIGGER before_insert_jieyue ON [dbo].[借阅信息表] for INSERT,update AS BEGIN IF exists(select 状态 from dbo.读者信息表 where 读者编号=(select 读者编号 from inserted)) not in('正常') PRINT '账号不正常' ROLLBACK END
[其他解释] CREATE TRIGGER before_insert_jieyue ON [dbo].[借阅信息表] for INSERT,update AS BEGIN IF exists(select 状态 from dbo.读者信息表 where 读者编号=(select 读者编号 from inserted)) not in('正常') --红字的等号换成in会不会好点?inserted表里面一定是只有一条数据? PRINT '账号不正常' ROLLBACK END [其他解释]
换成in也不对,错误如楼上 [其他解释] CREATE TRIGGER before_insert_jieyue ON [dbo].[借阅信息表] for INSERT,update AS BEGIN IF exists(select 状态 from dbo.读者信息表 where 读者编号=(select 读者编号 from inserted) and 状态 <>'正常') PRINT '账号不正常' ROLLBACK END