Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3806228
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:48:09+00:00 2026-05-19T14:48:09+00:00

Background: There’s a stored procedure which does stuff with a temp table of a

  • 0

Background: There’s a stored procedure which does “stuff” with a temp table of a given name. The procedure is generic in that it inspects the schema of the temp table and then does different “stuff” depending on the schema. I understand that this is a little weird but I’m reluctant to change it because it all works fine in most situations, except….

If I have a stored procedure which creates two different schemas for a temp table with the same name. Logically it only creates one temp table depending on which branch of the IF. The problem is that when the Sproc is checked by SQL Server it seems like it is evaluating both sides of the IF (which makes sense if it’s checking the SQL syntax.)

So this SQL fails:

IF (1=1)
BEGIN
    CREATE TABLE #test
    (
        a BIGINT NOT NULL,
        b BIGINT NOT NULL
    )
END 
ELSE
BEGIN
    CREATE TABLE #test
    (
        a BIGINT NOT NULL,
        b BIGINT NOT NULL,
        c BIGINT NOT NULL   
    )   
END

--exec SomeProcedureWhichDoesStuffWith#Test

DROP TABLE #test 

with the following error:

Msg 2714, Level 16, State 1, Line 14
There is already an object named
‘#test’ in the database.

No combination of drop table inside the ifs (before or after the create table DDL) seems to satisfy the sql checker.

Any ideas how I can do this? Can I, for example, tell SQL to not perform the syntax check and just accept the sproc as is?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:48:09+00:00Added an answer on May 19, 2026 at 2:48 pm

    It’s a limitation. Dynamic SQL won’t work either since #tmp will be created in a new session and immediately lost. For the EXACT snippet as shown, this does the same

    CREATE TABLE #test
    (
        a BIGINT NOT NULL,
        b BIGINT NOT NULL
    )
    
    IF not (1=1)
        ALTER TABLE #test ADD c BIGINT NOT NULL   
    

    There cannot be two CREATE .. #name within the same batch, but this will also work in general form

    IF (1=1)
    BEGIN
        CREATE TABLE #test
        (
            a BIGINT NOT NULL,
            b BIGINT NOT NULL
        );
    END 
    GO
    
    IF NOT (1=1)
    BEGIN
        CREATE TABLE #test
        (
            a BIGINT NOT NULL,
            b BIGINT NOT NULL,
            c BIGINT NOT NULL   
        )
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.