存储 INSERT 查询 FROM 子句结果列中的 SQL 变量
这对于大多数人来说可能是微不足道的,但我编写存储过程的时间并不长(仅 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
This is probably trivial to most of you, but I haven't been writing stored procedures for very long (6 months only). I'd like to be able to set the variable @testid based on one of the columns being used for an INSERT query. How can I do this?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
INSERT..SELECT.. 本质上是多行,因此它不允许语义允许将值分配给标量变量:应该将哪一行用于该值?
An INSERT..SELECT.. is inherently multirow so it doesn't make semse to allow assigning a value to a scalar variable: what row should be used for the value?