在触发器内从 DELETED 表中选择特定行
我有一个中间表,它定义了 Customer
和 Orders
之间的多对多关系,如下所示:
USE [master]
GO
CREATE DATABASE Example
GO
USE [Example]
GO
CREATE TABLE [dbo].[CustomerOrders](
[CustomerId] [int],
[OrderId] [int]
)
GO
INSERT INTO CustomerOrders (CustomerId, OrderId) VALUES (1, 1)
INSERT INTO CustomerOrders (CustomerId, OrderId) VALUES (1, 2)
INSERT INTO CustomerOrders (CustomerId, OrderId) VALUES (2, 1)
GO
CREATE TRIGGER [dbo].[CustomerOrdersRemoved]
ON [dbo].[CustomerOrders]
FOR DELETE
AS
BEGIN
SET NOCOUNT ON;
--IF NOT EXISTS (SELECT CustomerId FROM CustomerOrders
INNER JOIN deleted ON CustomerOrders.CustomerId=deleted.CustomerId)
--this wont work
END
GO
DELETE CustomerOrders WHERE OrderId=1
GO
现在,我需要有一个 ON此表上的 DELETE
触发器,这需要根据表中没有任何订单的客户更新另一个表。在这种情况下,在执行 DELETE
操作后,CustomerId=1
的客户将有 1 个带有 OrderId=2
的订单,而 的客户将有 1 个订单。 CustomerId=2
将没有剩余订单。因此,我需要从触发器内的已删除 vtable
中仅获取具有 CustomerId=2
的客户。
我怎样才能做到这一点?
I have an intermediate table that defines many-to-many relationship between, for instance, Customer
and Orders
, like this:
USE [master]
GO
CREATE DATABASE Example
GO
USE [Example]
GO
CREATE TABLE [dbo].[CustomerOrders](
[CustomerId] [int],
[OrderId] [int]
)
GO
INSERT INTO CustomerOrders (CustomerId, OrderId) VALUES (1, 1)
INSERT INTO CustomerOrders (CustomerId, OrderId) VALUES (1, 2)
INSERT INTO CustomerOrders (CustomerId, OrderId) VALUES (2, 1)
GO
CREATE TRIGGER [dbo].[CustomerOrdersRemoved]
ON [dbo].[CustomerOrders]
FOR DELETE
AS
BEGIN
SET NOCOUNT ON;
--IF NOT EXISTS (SELECT CustomerId FROM CustomerOrders
INNER JOIN deleted ON CustomerOrders.CustomerId=deleted.CustomerId)
--this wont work
END
GO
DELETE CustomerOrders WHERE OrderId=1
GO
Now, I need to have an ON DELETE
trigger on this table, which would need to update another table based on customers who don't have any orders left in the table. In this case, after the DELETE
operation the customer with CustomerId=1
will have 1 order with OrderId=2
left and customer with CustomerId=2
will have no orders left. So I need to get only customer with CustomerId=2
from the deleted vtable
within the trigger.
How can I accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您在比较的右侧使用deleted.CustomerId。
我正在做类似于你正在做的事情。
解决方案是定义一个@variable来保存deleted.CustomerId(在您的SET NO COUNT行之后),然后将CustomerOrders.CustomerId与该变量进行比较。
编辑:
这是工作得很好的代码,它是一个触发器,通过从 tblRating(MovieID, UserID, rating) 获得的平均评级来更新电影评级,这也表示 tblMovies 和 tblUsers 之间的多对多关系,这个使用插入但与删除是一样的:
The problem is that you're using deleted.CustomerId on the right side of a comparison.
I was doing something similar to what you're doing.
and the solution is to define a @variable to hold deleted.CustomerId (After your SET NO COUNT line), then compare CustomerOrders.CustomerId to the variable.
EDIT:
Here's the code which works just fine, It is a trigger to update movie rating by the average rating it got from tblRating(MovieID, UserID, rating) which also represents a many-to-many relationship between tblMovies and tblUsers, this one uses inserted but it is the same with deleted:
我的手比我的头快:-/
答案是
SELECT CustomerId FROM returned WHERE CustomerId NOT IN (SELECT CustomerId FROM CustomerOrders)
My hands are faster than my head :-/
The answer is
SELECT CustomerId FROM deleted WHERE CustomerId NOT IN (SELECT CustomerId FROM CustomerOrders)