Mik*_*son 15
号
的变量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)
Run Code Online (Sandbox Code Playgroud)
结果
-100
20
Run Code Online (Sandbox Code Playgroud)