我有一个proc执行select和update语句,如下所示。我需要合并update语句和select语句,它将数据放入临时表中成为单个sql select语句。
这可能吗?
'
答案 0 :(得分:0)
是:
CREATE TABLE TableA
(
valueA int
)
INSERT INTO
TableA
VALUES
(1),
(2),
(3)
GO
CREATE PROCEDURE test_procedure (@in_value int)
AS
BEGIN
--insert into temp table
SELECT
@in_value [out_value]
INTO
#TestTable
-- update with join on temp table
UPDATE
T
SET
T.[out_value] = 2
FROM
#TestTable T
INNER JOIN TableA A on A.valueA = T.out_value
WHERE
A.valueA = 1
-- update with join on temp table again
UPDATE
T
SET
[out_value] = 3
FROM
#TestTable T
INNER JOIN TableA A on A.valueA = T.out_value
WHERE
A.valueA = 2
--select results including the original "in_value"
SELECT
*,
@in_value [in_value]
FROM
TableA A
LEFT JOIN #TestTable T on T.out_value = A.valueA
END;
GO
--execute stored procedure
EXEC test_procedure 1
答案 1 :(得分:0)
我通过使用union找到了一条出路。 这有点奇怪,但因为我无法找到任何其他东西,用这种方法解决了