如何检查变量是否已经声明(T-SQL)? [英] How to check if a variable is already declared (T-SQL)?

查看:33
本文介绍了如何检查变量是否已经声明(T-SQL)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我写

DECLARE @x INT

有没有办法检查变量@x 是否已经声明?

Is there a way to check whether the variable @x has already been declared or not?

推荐答案

No.
tsql 中变量的声明不像其他语言那样遵循代码路径和使用范围.

No.
The declaration of variables in tsql does not follow the code path and use scope like perhaps other languages does.

此代码显示 @xx 存在但未分配,即使从未执行过声明.

This code shows that @xx exists but is unassigned even though the declaration was never executed.

if 1 = 0 
begin
  declare @xx int = 10
end
else
begin
  declare @yy int = 20
end

print coalesce(@xx, -100)
print coalesce(@yy, -200)

结果

-100
20

这篇关于如何检查变量是否已经声明(T-SQL)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆