第一行似乎是正确的。第二行是我的SQLite代码。使用代码我得到一个关于触发器附近的错误的例外。在VS中,它表示无法绑定多部分标识符。使用SQLite,新的代表插入行。所以我想增加订阅者的数量。我如何使用SQL Server触发器执行此操作?
CREATE TRIGGER trig_0 ON subscription
AFTER INSERT AS
UPDATE user_data
SET msg_count = msg_count + 1
WHERE id = new.recipient;
答案 0 :(得分:7)
SQL Server中没有神奇的“新”。有一个神奇的INSERTED,是一个表:
CREATE TRIGGER trig_0 ON subscription
AFTER INSERT AS
UPDATE user_data
SET msg_count = msg_count + 1
FROM user_data
JOIN INSERTED ON id = INSERTED.recipient;
答案 1 :(得分:1)
我认为触发器使用的inserted and delted
表不是新的'''
3> CREATE TRIGGER myTriggerINSERT
4> ON Employee
5> FOR INSERT
6> AS
7> DECLARE @ID int, @Name nvarchar(30)
8>
9> SET @ID = (SELECT ID FROM inserted)
10> SET @Name = (SELECT Name FROM inserted)
了解更多详情:http://www.java2s.com/Code/SQLServer/Trigger/Getvaluefromupdatedinsertedanddeleted.htm
答案 2 :(得分:0)
假设订阅具有可以与user_data.id连接的列收件人,这里有一种方法,您可以使用插入的伪表加入
CREATE TRIGGER trig_0 ON subscription
AFTER INSERT AS
UPDATE user_data
SET msg_count = msg_count + 1
WHERE exists (Select * from inserted i where user_data.id = inserted.recipient)