gpt4 book ai didi

sql - 无法绑定(bind)多部分标识符 "INSERTED. "

转载 作者:行者123 更新时间:2023-12-05 09:23:04 25 4
gpt4 key购买 nike

我想写一个触发器:如果表中存在记录,则更新另一个表,如果不存在则插入该表创建此触发器时出错:

CREATE TRIGGER TR_update_laststatus_on_membersservices ON [dbo].[activemembers] AFTER 
INSERT AS
BEGIN
IF EXISTS
(
SELECT 1
FROM [membersservices]
WHERE [membersservices].[mobilenumber] = INSERTED.[mobilenumber]
AND [membersservices].[sid] = INSERTED.[sid]
)
BEGIN
UPDATE membersservices
SET membersservices.[last_status] = INSERTED.enable
WHERE membersservices.mobilenumber=INSERTED.mobilenumber and membersservices.[sid]=INSERTED.[sid]
END
ELSE
INSERT INTO membersservices VALUES ( INSERTED.[mobilenumber] ,INSERTED.[sid] ,INSERTED.[enable] , INSERTED.[dt] );
end

最佳答案

INSERTED 是一个(伪)表,因此它必须出现在 FROM 子句中,每次您使用它时:

SELECT 1   
FROM [membersservices]
JOIN INSERTED on ... <--- you're missing this
WHERE [membersservices].[mobilenumber] = INSERTED.[mobilenumber]
AND [membersservices].[sid] = INSERTED.[sid]


UPDATE membersservices
SET membersservices.[last_status] = INSERTED.enable
FROM membersservices
JOIN INSERTED ON ... <-- you're missing this


INSERT INTO membersservices (...)
SELECT ...
FROM INSERTED ... <-- you're missing this

除此之外,您的逻辑在并发下是不正确的。您应该使用单个 MERGE执行更新和插入的语句。

关于sql - 无法绑定(bind)多部分标识符 "INSERTED. ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24259281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com