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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:13:05+00:00 2026-05-16T06:13:05+00:00

I am fairly new to SQL, so not sure if this is just a

  • 0

I am fairly new to SQL, so not sure if this is just a SQL compiler thing or not. When I have used SQL before it’s been through JAVA or PHP, never just straight SQL. I am using SQL Server 2005 and attempting to add a column to my table, and then populate it. Here is what I have now:

ALTER TABLE House DROP COLUMN CustomerType
ALTER TABLE House ADD CustomerType char(11)

UPDATE House
SET CustomerType = CASE
    WHEN ... THEN...
    WHEN ... THEN...
    ELSE "Other"
    END

However, when I try and compile this it errors since CustomerType in the UPDATE function hasn’t been defined yet. Is there any way to run this so that it will compile and add the column and update it, or do I have to run it in multiple executes?

Thanks!

  • 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-16T06:13:06+00:00Added an answer on May 16, 2026 at 6:13 am

    Use multiple files for each operation, or put “GO” between the steps:

    ALTER TABLE House DROP COLUMN CustomerType
    GO
    
    ALTER TABLE House ADD CustomerType char(11)
    GO
    
    UPDATE House
    SET CustomerType = CASE
        WHEN ... THEN...
        WHEN ... THEN...
        ELSE "Other"
        END
    GO
    

    This works in SQL Server Management Studio – it’s not a T-SQL features, but a feature of Mgmt Studio to separate the T-SQL “batches” of SQL commands.

    Update: if you want to make your script so that it can be run multiple times, the more useful approach would be to check for the column’s existance, and only add it if it doesn’t exist yet – no point in always dropping and re-adding it, if it’s already there!

    To do that, use something like:

    IF NOT EXISTS(SELECT * FROM sys.columns WHERE name = 'CustomerType' 
                           AND object_id = OBJECT_ID('House'))
       ALTER TABLE dbo.House ADD CustomerType CHAR(11)
    GO
    

    This snippet of code checks the SQL catalog view sys.columns to see if that column already exists in that table – if not, it’s created. When you run this code against your table a thousand times, the first time around, the column gets created, and any subsequent run will do nothing – since that column already exists. Much cleaner than constantly dropping and re-adding a column!

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

Sidebar

Related Questions

I am fairly new to Emacs and I have been trying to figure out
I'm still a newbie to Adobe Air/Flex, and still fairly new with SQL. I've
I'm still fairly new to T-SQL and SQL 2005. I need to import a
I'm sure this is something really simple that I'm overlooking, but MS SQL is
I'm working on a fairly new project and we started from scratch. So not
I'm fairly new to sql and was hoping that someone can help me with
I am fairly new to SSAS and SQL Server but Google didn't help. I
Being fairly new to JavaScript, I'm unable to discern when to use each of
I'm fairly new to ASP.NET and trying to learn how things are done. I
I'm fairly new to the STL, so I was wondering whether there are any

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.