如何使用 T-SQL 在一行中设置多个局部变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8269054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set multiple local variables in one line using T-SQL?
提问by The Light
declare @inserted bit
declare @removed bit
I know it's possible to set them like below:
我知道可以像下面这样设置它们:
SELECT @inserted = 0, @removed = 0
but would it be possible to make this even simpler and use something like:
但是是否有可能使这更简单并使用以下内容:
SET @inserted, @removed = 0
Many thanks
非常感谢
回答by marc_s
How about:
怎么样:
declare @inserted BIT = 0, @removed BIT = 0
Works in SQL Server 2008 and up (you didn't specify which versionof SQL Server....)
适用于 SQL Server 2008 及更高版本(您没有指定SQL Server 的哪个版本......)
Update:ok, so you're stuck on SQL Server 2005 - in that case, I believe this is the best you can do:
更新:好的,所以你被困在 SQL Server 2005 上 - 在这种情况下,我相信这是你能做的最好的:
DECLARE @inserted BIT, @removed BIT
SELECT @inserted = 0, @removed = 0
回答by Tomalak
but would it be possible to make this even simpler and use something like:
SET @inserted, @removed = 0
但是是否有可能使这更简单并使用以下内容:
SET @inserted, @removed = 0
I suppose yo mean
我想你的意思是
SET @inserted = @removed = 0
No, that is not possible. T-SQL does not support this kind of syntax.
不,那是不可能的。T-SQL 不支持这种语法。