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 7838213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:02:21+00:00 2026-06-02T15:02:21+00:00

Given the following SQL: IF EXISTS (SELECT * FROM sys.columns WHERE name = ‘NewFieldName’

  • 0

Given the following SQL:

IF EXISTS (SELECT * FROM sys.columns WHERE name = 'NewFieldName' AND object_id = OBJECT_ID('dbo.MyTableName'))
    RETURN

-- Add NewFieldName column to part of the Summer 2012 release cycle.
ALTER TABLE dbo.[MyTableName] ADD
    [NewFieldName] SmallINT NOT NULL
        CONSTRAINT DF_MyTableName_NewFieldName DEFAULT (2)

UPDATE [MyTableName] SET NewFieldName = 1 WHERE [Name] = 'FindMe' --Update one specific value

Produces the following error message:

Msg 207, Level 16, State 1, Line 10 Invalid column name
‘NewFieldName’.

I’m sure I’m missing something basic, but trying to put “GO” after the alter makes the UPDATE run everytime and I don’t want to do that.

How can I structure this statement so that it will check to see if the column exists and, if it doesn’t add it and then set the values as stated in my UPDATE statements?

  • 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-06-02T15:02:22+00:00Added an answer on June 2, 2026 at 3:02 pm

    You need the statement referencing the new column to be compiled after the new column is added. One way of doing this is to run it as a child batch with EXEC.

    IF NOT EXISTS (SELECT * 
                   FROM   sys.columns 
                   WHERE  name = 'NewFieldName' 
                          AND object_id = OBJECT_ID('dbo.MyTableName')) 
    BEGIN
      -- Add NewFieldName column to part of the Summer 2012 release cycle. 
      ALTER TABLE dbo.[MyTableName] 
               ADD [NewFieldName] SMALLINT NOT NULL 
               CONSTRAINT DF_MyTableName_NewFieldName DEFAULT (2) 
    
      EXEC(' UPDATE [MyTableName] SET NewFieldName = 1 WHERE [Name] = ''FindMe''') 
    END
    

    The reason it worked for you originally is presumably because the table itself did not exist when the batch was compiled thus meaning that all statements in it referencing the table are subject to deferred compile.

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

Sidebar

Related Questions

I've picked up some SQL similar to the following: IF EXISTS(SELECT name FROM tempdb..sysobjects
I have following SQL code: SELECT m.email FROM members as m WHERE exists (
Given following Statment: String query = "Select * from T_spareParts where SparePartPK IN (?
I have the following SQL query I was given, but I am not sure
Given the following schema / data / output how would I format a SQL
Given the following HTML <b>demo</b> I want to save it to an SQL 2005
Given the following table id parentID name image 0 0 default.jpg 1 0 Jason
Given the following connection string (for SQL Server CE 4.0) Data Source=|DataDirectory|IntegrationTests.sdf how do
Given the following simple table structure (SQL Server 2008), I want to be able
I'm struggling to convert the following (simplified) HQL to QueryOver: select subscription from Subscription

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.