Question : How to validate the column name?

Dear All,

I want to validate whether a column name user enters is valid or not in the database xxx.

I know we could use "select * from sysobjects where xtype='U' and name = 'yyy'" to validate a TABLE name. But how can we do for a COLUMN?


Thanks heaps in advance!

Answer : How to validate the column name?

You should know that it is very rarely a good idea to use the system tables directly.  But if you want to do it that way then:

IF EXISTS ( SELECT  1
            FROM    sysobjects o
                    INNER JOIN syscolumns c ON o.id = c.id
            WHERE   o.xtype = 'U'
                    AND o.name = 'yyy'
                    AND c.name = 'YourColumnNameGoesHere' )
    PRINT 'Column exists!'
Random Solutions  
 
programming4us programming4us