[ACCEPTED]-How to check if a variable is already declared (T-SQL)?-tsql
Accepted answer
No.
The declaration of variables in tsql 4 does not follow the code path and use scope 3 like perhaps other languages does.
This code 2 shows that @xx
exists but is unassigned even 1 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)
Result
-100
20
IF you try to access a variable that has 4 not yet been defined, the T-SQL Script will 3 give you an error telling you the variable 2 isn't defined.
Msg 137, Level 15, State 2, Line 1 5 Must declare the scalar variable "@x".
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.