tsql – 如何检查变量是否已被声明(T-SQL)?

当我写
DECLARE @x INT

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

解决方法

没有.
tsql中变量的声明不符合代码路径,也不像其他语言那样使用范围.

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

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

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...