日期:2014-05-17  浏览次数:20439 次

我写的sql,如果读者信息表中状态不是正常,就不能在借阅表中添加记录
CREATE TRIGGER before_insert_jieyue
ON [dbo].[借阅信息表]
for INSERT,update
AS BEGIN
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
------其他解决方案--------------------
引用:
CREATE TRIGGER before_insert_jieyue
ON [dbo].[借阅信息表]
for INSERT,update
AS BEGIN
IF exists(select 状态 
from dbo.读者信息表
where 读者编号=(select 读者编号 from inserted)) not in('正常') 
    PRINT ……

还是不对,错误如下:

消息 102,级别 15,状态 1,过程 before_insert_jieyue,第 1 行
' ' 附近有语法错误。
消息 102,级别 15,状态 1,过程 before_insert_jieyue,第 1 行
')' 附近有语法错误。


------其他解决方案--------------------
引用:
CREATE TRIGGER before_insert_jieyue
 ON [dbo].[借阅信息表]
 for INSERT,update
 AS BEGIN