Check for existing column of a table

  • How can I check to see if a column exists in a table by using

    the MS system store procedures?  

  • With many, sp_help, sp_columns, etc.

    You can also use a query like this:

    IF EXISTS (SELECT 1 FROM syscolumns where name = 'ColName' AND OBJECT_NAME(id) = 'TableName') PRINT 'Exists' ELSE PRINT 'no exists'

  • Thanks for the reply. I have another question:

    If I use the following:

    EXEC sp_columns @table_name = 'X', @column_name = 'YY'

    How do I check if the result set returned is empty or not.

     

     

  • After the execution of the procedure, you can check @@ROWCOUNT

    EXEC sp_columns @table_name='X', @column_name='YY'

    IF @@ROWCOUNT>0

    BEGIN

    Insert code here

    END

     

    Brian

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

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