我正在尝试根据列表中的每个Id更新一些值。我的逻辑似乎就是我想要的。
我想填充一个临时的Ids表。然后,对于我想要应用此查询的每个ID,并将删除的日期和ID输出到我创建的新表中。
我一直收到错误:
Msg 10716, Level 15, State 1, Line 25
A nested INSERT, UPDATE, DELETE, or MERGE statement must have an OUTPUT clause.
这是什么意思?我以为我正在输入我创建的新表。
USE datatemp
GO
DECLARE @idlist TABLE (id INT)
INSERT INTO @idlist (id) VALUES (3009099)
DECLARE @EndDate DATETIME
SET @EndDate = '2099-12-12'
IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'TEMP_TABLE')
BEGIN
CREATE TABLE [TEMP_TABLE] (
[id] INT,
[thedatetoend] DATETIME);
END
BEGIN TRY
SELECT *
FROM @idlist AS idlist
OUTER APPLY(
UPDATE [custprofile]
SET thedatetoend = @EndDate
OUTPUT idlist.id, DELETED.thedatetoend
INTO [TEMP_TABLE]
FROM [custprofile] as bc
INNER JOIN [custinformation] as cc
ON cc.custengageid = bc.custengageid
WHERE cc.id = idlist.id
AND bc.modifierid = 2
AND bc.thedatetoend > GETDATE()
AND cc.type = 1) o
答案 0 :(得分:0)
我认为您可以通过使用CTE并避免使用当前使用的外部应用方法获得更多成功。对CTE级联到源表的更新。它可能看起来像下面的内容,但由于有些专栏没有引用表别名,所以不希望这种情况发挥作用"因为" (即我不确定你是输出ccid还是bcid而且我不知道该日期属于哪个表。)
WITH
CTE AS (
SELECT
cc.id AS ccid, bcid AS bcid, thedatetoend
FROM [custprofile] AS bc
INNER JOIN [custinformation] AS cc ON cc.custengageid = bc.custengageid
INNER JOIN @idlist AS idlist ON cc.id = idlist.id
WHERE bc.modifierid = 2
AND bc.thedatetoend > GETDATE()
AND cc.type = 1
)
UPDATE CTE
SET thedatetoend = @EndDate
OUTPUT ccid, DELETED.thedatetoend
INTO [TEMP_TABLE]