SQL Server emergency? Email pinal@sqlauthority.com with URGENT in the subject line.

COL_NAME Function in SQL Server: Get a Column Name by ID

The COL_NAME function returns a column’s name when you give it a table’s object ID and the column’s ID. This post uses it to turn the column IDs in index views into names, shows why a column ID is not a position once a column has been dropped, and explains the trap that returns a wrong name when the table lives in another database.

Continue Reading

How to Check if a Column Exists in a SQL Server Table

To check if a column exists in a SQL Server table, call COL_LENGTH and test the result for NULL, or look the column up in sys.columns. This post compares the three common checks, including the schema trap in INFORMATION_SCHEMA and the three-part name for another database. It ends with a script that adds a column only when it is missing, and the batch rule that makes that script fail if you ignore it.

Continue Reading