这对大多数人来说可能是微不足道的,但我并没有长时间编写存储过程(仅限6个月)。我希望能够根据用于INSERT查询的其中一列设置变量@testid。我怎么能这样做?
DECLARE @testid INT;
INSERT INTO [exporttestresultreport] (
[testid],
[othercolumn]
)
SELECT
[testid], -- <======= how can I set my variable based on this column?
[othercolumn]
FROM
[exporttestresultreport] e
WHERE
[exporttestresultreportid] = @exporttestresultreportid
答案 0 :(得分:3)
DECLARE @testid INT;
DECLARE @test TABLE (testid int);
INSERT INTO [exporttestresultreport] (
[testid],
[othercolumn]
)
OUTPUT INSERTED.testID INTO @test
SELECT
[testid], -- <======= how can I set my variable based on this column?
[othercolumn]
FROM
[exporttestresultreport] e
WHERE
[exporttestresultreportid] = @exporttestresultreportid;
SELECT @testid = testid FROM @test;
INSERT..SELECT ..本质上是多行的,因此允许为标量变量赋值是没有意义的:值应该用于哪一行?
答案 1 :(得分:1)
DECLARE @testid INT;
DECLARE @t TABLE(t INT);
INSERT exporttestresultreport
(
testid, othercolumn
)
OUTPUT INSERTED.testid INTO @t
SELECT testid, othercolumn
FROM
[exporttestresultreport] e
WHERE
[exporttestresultreportid] = @exporttestresultreportid;
SELECT @testid = t FROM @t;
-- not sure what you want to do if there are multiple rows in the insert