如何从内部停止执行触发器?

时间:2014-11-13 03:25:45

标签: sql-server triggers insert-into

这是代码。它是在插入

之后执行的触发器
 ALTER TRIGGER [dbo].[GameStacker]
 ON [dbo].[CDKeyUser]
 AFTER INSERT AS 
 BEGIN
    INSERT INTO User_Achivement
            ( User_ID ,
            Achivement_ID,
            Date 
            )
    VALUES  ((SELECT User_ID from inserted),
            (SELECT 
                CASE
                    WHEN COUNT(cd.User_ID)=1 THEN   1
                    WHEN COUNT(cd.User_ID)=5 THEN   2
                    WHEN COUNT(cd.User_ID)=10 THEN  3
                    WHEN COUNT(cd.User_ID)=15 THEN  4
                END
            FROM CDKeyUser cd
            WHERE cd.User_ID=(SELECT User_ID from inserted)
            ),                  
            GETDATE()
            )
 END

所以我的问题是这个触发器在每次插入后执行。(我知道它应该是如何工作的。)但是,如果WHEN COUNT(cd.User_ID)的值不是这里指定的值,它应该不执行,或只是打破执行。如果我在WHEN COUNT(cd.User_ID)=15 THEN 4之后添加一个else语句,我不能包含任何代码,例如raiseerror,因为它被转换为Achivement_ID的值,它给了我一个错误。 所以基本上我必须打破insert语句的执行。

如果有人可以帮我解决这个问题,我真的很感激

3 个答案:

答案 0 :(得分:1)

尝试这样的事情。

IF EXISTS (SELECT Count(A.User_ID) cnt
           FROM   CDKeyUser A
                  JOIN inserted B
                    ON a.User_ID = b.User_ID
           HAVING Count(A.User_ID) IN ( 1, 5, 10, 15 ))
  BEGIN
      INSERT INTO User_Achivement
                  (User_ID,Achivement_ID,Date)
      VALUES      ((SELECT User_ID
                    FROM   inserted),(SELECT CASE
                                               WHEN Count(cd.User_ID) = 1 THEN 1
                                               WHEN Count(cd.User_ID) = 5 THEN 2
                                               WHEN Count(cd.User_ID) = 10 THEN 3
                                               WHEN Count(cd.User_ID) = 15 THEN 4
                                             END
                                      FROM   CDKeyUser cd
                                      WHERE  cd.User_ID = (SELECT User_ID
                                                           FROM   inserted)),Getdate() )
  END 

答案 1 :(得分:0)

尝试:

if (select count(cd1.User_ID) 
    FROM CDKeyUser cd1
    WHERE cd1.User_ID=(SELECT User_ID from inserted)) IN (1, 5, 10, 15)
begin
    --your insert statement
end

完整代码:

ALTER TRIGGER [dbo].[GameStacker]
 ON [dbo].[CDKeyUser]
 AFTER INSERT AS 
 BEGIN
 if (select count(cd1.User_ID) 
    FROM CDKeyUser cd1
    WHERE cd1.User_ID=(SELECT User_ID from inserted)) IN (1, 5, 10, 15)
 begin
    INSERT INTO User_Achivement
            ( User_ID ,
            Achivement_ID,
            Date 
            )
    VALUES  ((SELECT User_ID from inserted),
            (SELECT 
                CASE
                    WHEN COUNT(cd.User_ID)=1 THEN   1
                    WHEN COUNT(cd.User_ID)=5 THEN   2
                    WHEN COUNT(cd.User_ID)=10 THEN  3
                    WHEN COUNT(cd.User_ID)=15 THEN  4
                END
            FROM CDKeyUser cd
            WHERE cd.User_ID=(SELECT User_ID from inserted)
            ),                  
            GETDATE()
            )
  end
  END

答案 2 :(得分:0)

ALTER TRIGGER [dbo].[GameStacker]
 ON [dbo].[CDKeyUser]
 AFTER INSERT AS 
 BEGIN
 if (selet COUNT(User_ID) from CDKeyUser)<=15
 begin
 'your code'
 END
 end