除了使用游标之外,还有什么方法可以将SELECT
结果用于后续INSERT/UPDATE
查询?
就像是:
DECLARE @SELECTRESULT;
SELECT Something into @SELECTRESULT
FROM Somewhere
INSERT INTO SomewhereElse (X, XX, XXX)
SELECT Something, GETDATE(), 'XXX'
FROM @SELECTRESULT
UPDATE Somewhere
Set SomethingElse = 'ABC'
WHERE
Something in
(SELECT Something FROM @SELECTRESULT)
原因是我有一个来自多个表的相对复杂的查询,我不想重复这段代码,一次用于插入,第二次用于更新。