Drop temp table within IF ELSE statement

后端 未结 1 888
名媛妹妹
名媛妹妹 2020-12-07 05:41

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

相关标签:
1条回答
  • 2020-12-07 06:20

    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.

    0 讨论(0)
提交回复
热议问题