我们有两个表:一个包含产品,另一个包含订单信息。
CREATE TABLE [dbo].[ORDERS]
(
[ROW] [BIGINT] IDENTITY(1, 1) NOT NULL,
[ID] [UNIQUEIDENTIFIER] NOT NULL,
[RETAILER_ID] [UNIQUEIDENTIFIER] NOT NULL,
[INDIVIDUAL_ID] [UNIQUEIDENTIFIER] NOT NULL,
[PRODUCT_ID] [UNIQUEIDENTIFIER] NOT NULL,
[QUANTITY] [BIGINT] NOT NULL,
[DATE] [DATETIME] NOT NULL,
[MEMO] [NVARCHAR](MAX) NULL
)
ON [PRIMARY]
TEXTIMAGE_ON [PRIMARY]
CREATE TABLE [dbo].[PRODUCTS]
(
[ROW] [BIGINT] IDENTITY(1, 1) NOT NULL,
[ID] [UNIQUEIDENTIFIER] NOT NULL,
[RETAILER_ID] [UNIQUEIDENTIFIER] NOT NULL,
[NAME] [NVARCHAR](255) NOT NULL
)
ON [PRIMARY]
我们需要修复此触发器,以便在产品名称发生更改时,订单表中的备注字段需要在订单表中对该更改进行备注。所以这是我们设计的触发器,但是我们遇到了错误"多部件标识符" o.Memo"无法受约束。"
这是产生错误的触发器?我们哪里出错?
CREATE TRIGGER DBO.PRODUCTS_NAME_CHG
ON DBO.PRODUCTS
AFTER UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
DECLARE @old NVARCHAR(255),
@new NVARCHAR(255),
@ID UNIQUEIDENTIFIER
SELECT @ID = ID,
@old = NAME
FROM DELETED
SELECT @NEW = NAME
FROM INSERTED
SET NOCOUNT ON;
IF UPDATE (NAME)
BEGIN
UPDATE DBO.ORDERS
SET o.MEMO = o.MEMO + ' ' + @OLD + ' HAS CHANGED NAME TO ' + @NEW
+ '. '
FROM ORDERS o
INNER JOIN PRODUCTS P
ON P.ID = O.PRODUCT_ID
WHERE P.ID = @ID
END
END
GO
答案 0 :(得分:3)
嗯,内部联接更新的语法应该是
UPDATE o -- use alias here, not table name.
SET o.Memo = --blabla
FROM Orders o
INNER JOIN --blabla