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 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 have the following SQL statement which returns Not unique table/alias . I'm fairly
I am fairly new to MySQL with Java, but I have executed a few
I'm fairly new to coding in T-SQL and have run into an issue I
I'm fairly sure this is an easy thing to do, but I'm a newbie
I'm still fairly new to SQL and I'm not fully understanding where the problem
I'm working with a SQL database (still fairly new to it) and have attempted
I'm fairly new to SQL, but trying to write quite a complicated query, but
Oracle's PL/SQL is fairly new to me, so I need some help understanding if
Alright, so I'm fairly new to PHP and SQL/MySQL so any help is appreciated.
Fairly new to programming. I just can't wrap my head around how to get

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.