我试图从1个表中获取信息并将其插入另一个表的记录中。但每次我尝试使用WHERE子句时,我都会收到此错误。我需要WHERE,因为我需要第一个表中的值与第二个表中的相同记录具有相同的报价编号。
我的代码:
ALTER TRIGGER InsertQuoteNumber
ON AccountInfo
FOR INSERT
AS
DECLARE @ShippingIdentity nvarchar(50)
DECLARE @QuoteNumber nvarchar(50)
SELECT @QuoteNumber = QuoteNumber FROM AccountInfo
BEGIN
UPDATE ShippingInfo SET @QuoteNumber = QuoteNumber
WHERE AccountInfo.ShippingIdentity = ShippingInfo.ShippingIdentity
END
答案 0 :(得分:1)
每次尝试使用WHERE子句时,我都会收到此错误。
错误在于:
UPDATE ShippingInfo SET @QuoteNumber = QuoteNumber
WHERE AccountInfo.ShippingIdentity --AccountInfo is not a part of query
= ShippingInfo.ShippingIdentity
事实上,你很幸运,你将更新所有行。你的触发器应该是这样的:
ALTER TRIGGER InsertQuoteNumber
ON AccountInfo
FOR INSERT
AS
--may make sense if you guarantee single row insert only
--DECLARE @ShippingIdentity nvarchar(50)
--DECLARE @QuoteNumber nvarchar(50)
--From What Row?
--SELECT @QuoteNumber = QuoteNumber FROM AccountInfo
BEGIN
UPDATE ShippingInfo SET QuoteNumber = i.QuoteNumber
from ShippingInfo s
inner join inserted i on s.ShippingIdentity = i.ShippingIdentity
--no **where** required
--WHERE AccountInfo.ShippingIdentity = ShippingInfo.ShippingIdentity
END