Check to see if column exsists

  • Hi there,

    I want to check if a column exists in a table and if it does remove/drop it .

    So the database is called "TEST" and the column is called "columnA"

    I know how to check the column and drop it separately.....but putting them both together I keep getting wrong.

    Thanks

  • Would this work?

    if object_id('Dataload_TEST..Table_DL') is not null

    ALTER TABLE Dataload_TEST..Table_DL DROP COLUMN LastChargeDate

    Actually this is just checking if the table exists - not if the column in that database exists.

  • This code checks for existence of the column and drops it.

    IF EXISTS (

    SELECT 1

    FROMsys.columns

    WHEREobject_Id = object_Id('Dataload_TEST..Table_DL')

    ANDname = 'LastChargeDate'

    )

    ALTER TABLE Dataload_TEST..Table_DL DROP COLUMN LastChargeDate;

    Louis.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply