I am facing a deadlock here, the issue is that I have to alter a procedure which makes use of 3 different temp tables. Lets for the sake of the conversation name them #tempt
The T-SQL parser is remarkably primitive. In particular, control flow doesn't affect when object names come into scope and remain in scope.
So the names you're using in your if
branch are still in scope and cause a conflict when the else
branch is parsed.
If possible, move the table definitions up to the top of the stored procedure, before any control flow, and switch to INSERT ... SELECT ...
rather than SELECT ... INTO ...
; or if the table definitions don't match between the if
and else
branches, you'll need to use different names for the temp tables.
As another example of how primitive the parser is, consider the following:
if 1=0
begin
declare @a int
end
select @a
This produces a result set containing null
rather than (as you might have expected) an error saying that @a
isn't declared.