我如何在触发器 SQL Server 中执行此操作

发布于 2024-09-02 07:08:50 字数 309 浏览 2 评论 0原文

第一行似乎是正确的。第二行是我的 SQLite 代码。通过代码,我得到了有关触发器附近错误的异常。在 VS 中,它说无法绑定多部分标识符。对于 SQLite,new 代表正在插入的行。所以我想增加订阅者的数量。我如何使用 SQL Server 触发器来做到这一点?

CREATE TRIGGER  trig_0  ON  subscription   
 AFTER INSERT  AS  
UPDATE user_data 
   SET msg_count = msg_count + 1 
 WHERE id = new.recipient; 

The first line seems to be correct. The second line was my SQLite code. With code i get an exception about an error near trigger. In VS it says the multipart identifier could not be bound. With SQLite the new stands for the row being insert. So i would like to increase the count of whomever the subscription recipient is. How do i do that with a SQL Server trigger?

CREATE TRIGGER  trig_0  ON  subscription   
 AFTER INSERT  AS  
UPDATE user_data 
   SET msg_count = msg_count + 1 
 WHERE id = new.recipient; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情愿 2024-09-09 07:08:50

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; 

There is no magic 'new' in SQL Server. There is a magic INSERTED, and is a table:

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; 
丢了幸福的猪 2024-09-09 07:08:50

我认为触发器使用了 inserted 和 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

i think there is inserted and delted tables used by the trigger not new '''

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)

for more detail : http://www.java2s.com/Code/SQLServer/Trigger/Getvaluefromupdatedinsertedanddeleted.htm

清醇 2024-09-09 07:08:50

假设订阅有列收件人,您可以使用 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) 

assuming subscription has the column recipient which you can join with user_data.id, here is one way, you can use the inserted pseudo table to join back

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) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文