我有这样的查询
CREATE TRIGGER check_shippeddate_tr
ON Orders
FOR UPDATE
AS
IF ((SELECT ShippedDate FROM inserted) > (SELECT RequiredDate FROM Orders) )
BEGIN
PRINT 'Order must be shipped before required date'
ROLLBACK TRANSACTION
END
GO
然而,当我尝试更新表时使用此代码
UPDATE Orders
SET ShippedDate = '1994-04-03'
WHERE OrderID = 11051
AND CustomerID = 'LAMAI'
AND EmployeeID = 7
它应该阻止更新并打印消息,因为ShippedDate位于RequiredDate之后。但是我收到了这个错误" 子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。"我该如何解决?
答案 0 :(得分:1)
尝试类似......
假设OrderID是主键列
CREATE TRIGGER check_shippeddate_tr
ON Orders
FOR UPDATE
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS (SELECT 1 FROM inserted i
inner join Orders o ON i.OrderID = o.OrderID
WHERE i.ShippedDate > O.RequiredDate)
BEGIN
PRINT 'Order must be shipped before required date'
ROLLBACK TRANSACTION
END
END
答案 1 :(得分:0)
在表格上创建一个检查约束
ALTER TABLE Orders
ADD CHECK (ShippedDate < RequiredDate)