我有一个CTE,它基本上是根据某些条件过滤数据的,但事实是,过滤之后,我需要更新这些记录以“纠正”它们,但是我在4104
语句中得到了UPDATE
当我直接调用变量时,我没有遇到任何问题,但是在进行计算的地方却出现了错误,因为我是从第一个查询和第二个查询中调用变量的。
;WITH CTE AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY timestmp ASC) order,
*
FROM TSTSOLAP T
(
--SECOND QUERY
SELECT 1 FROM TABLE1 T2
WHERE --CONDITIONS MET
t.user_id = t2.id_user AND
T.ID_TIP = T2.ID_TIP AND
T.ID <> t2.ID AND
(T2.BEG BETWEEN T.BEG AND T.END)
OR T2.BEG = T.END
)
)
UPDATE CTE SET STAT=0, END = (T2.END - 1) --HERE IS WHERE I GET THE: Msg 4104, Level 16, State 1, Line 1
WHERE ORDER> 1
GO
我怎么称呼非链接变量?
*由于长度缩短,代码缩短了
答案 0 :(得分:1)
只做JOIN
怎么样?
WITH toupdate AS (
SELECT . . . ,
? as order
FROM table1
)
UPDATE T
SET STAT = 0,
END = (T2.END - 1)
FROM T JOIN
table2 T2
ON . . . <conditions here>
WHERE T.ORDER > 1;
根据子查询中的条件,您可能仍然还需要EXISTS
子句。