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

  • SEARCH
  • Home
  • 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 6233797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:21:55+00:00 2026-05-24T10:21:55+00:00

If I try to execute the following code, I get the errors Msg 207,

  • 0

If I try to execute the following code, I get the errors

Msg 207, Level 16, State 1, Line 3 Invalid column name ‘Another’. Msg
207, Level 16, State 1, Line 4 Invalid column name ‘Another’.

even though the predicate for both IF statements always evaluates to false.

CREATE TABLE #Foo (Bar INT)
GO
IF (1=0)
BEGIN
    SELECT Another FROM #Foo
END
GO
IF (1=0)
BEGIN
    ALTER TABLE #Foo ADD Another INT
    SELECT Another FROM #Foo 
END
GO
DROP TABLE #Foo

This is probably over-simplified for the sake of the example; in reality what I need to do is select the values from a column, but only if the column exists. If it doesn’t exist, I don’t care about it. In the problem that drove me to ask this question, my predicate was along the lines of EXISTS (SELECT * FROM sys.columns WHERE object_id = @ID AND name = @Name). Is there a way to achieve this without resorting to my arch-enemy Dynamic SQL? I understand that my SQL must always be well-formed (i.e. conform to grammar) – even within a block that’s never executed – but I’m flabbergasted that I’m also being forced to make it semantically correct too!

EDIT:
Though I’m not sure the code below adds much to the code above, it’s a further example of the problem. In this scenario, I only want to set the value of Definitely (which definitely exists as a column) with the value from Maybe (which maybe exists as a column) if Maybe exists.

IF EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('dbo.TableName', 'U') AND name = 'Maybe')
BEGIN
    UPDATE dbo.TableName SET Definitely = Maybe
END
  • 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-24T10:21:55+00:00Added an answer on May 24, 2026 at 10:21 am

    SQL Server doesn’t execute line by line. It isn’t procedural like .net or Java code. So there is no “non-executed block”

    The batch is compiled in one go. At this point, the column doesn’t exist but it knows the table will be. Table does not have a column called “Another”. Fail.

    Exactly as expected.

    Now, what is the real problem you are trying to solve?

    Some options:

    • 2 tables or one table with both columns
    • use Stored procedures to decouple scope
    • not use temp tables (maybe not needed; it could be your procedural thinking…)
    • dynamic SQL (from Mitch’s deleted answer)

    Edit, after comment;

    Why not hide schema changes behind a view, rather than changing all code to work with columns that may/may not be there?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get the following error when I try and execute the code down below.
When I try to compile to following code, I get two errors: Description Resource
I get the following error when I try to execute a particular recursive CTE:
When i try execute the following code, which should just print a slashy string
I have the following code but I get an error on this line userSpinner.setAdapter(adapter);
When I try to execute my code, I'm getting an error dialog: 'Show Disassembly'
Try the following code public enum Color { Blue=1, Red=2, Green=3 } public List<Color>
I am using following code to write the PATH, EXECUTABLE NAME and ARGUMENTS to
I'm using the following code to try to read the results of a df
I have the following code: $bind = new COM(LDAP://CN=GroupName,OU=Groups,OU=Division,DC=company,DC=local); When I execute it from

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.