Results 1 to 3 of 3

Thread: If exists..

  1. #1
    Join Date
    Apr 2004
    Posts
    101

    Unanswered: If exists..

    Hi,
    Is there any way to check whether a column is there in the table, if it is there i need to drop it through script.

    i'm looking for the script, something like this..

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_Tbl_Product_Tbl_Products]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
    ALTER TABLE [dbo].[Tbl_Product] DROP CONSTRAINT FK_Tbl_Product_Tbl_Products
    GO

    In the same way i need to check for a column and drop it through script.
    Any help would be greatly appreciated.
    Thanks in advance.

  2. #2
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Look at the INFORMATION_SCHEMA.Columns View....

    But I would advise against any auto mucking
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  3. #3
    Join Date
    Apr 2004
    Posts
    101
    Hi Brett..
    Here is what i did..
    if exists (select * from information_schema.columns where table_name = 'tablename' and column_name = 'columnname')
    alter table tablename drop column columnname
    go
    alter table tablename add columnname varchar(255) null
    go

    It worked..
    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •