Skip to content

SQL - SQL Server - Check if a Column is Accessible

Source: sql-snippets/check-column-is-accessible.md at main · count/sql-snippets (github.com)

Explore this snippet here.

Description

One method to check if the current session has access to a column is to use the COL_LENGTH function, which returns the length of a column in bytes.

If the column doesn’t exist (or cannot be accessed), COL_LENGTH returns null.

SELECT col_length('Orders', 'Sales') AS does_exist,
       col_length('Orders', 'Shmales') AS does_not_exist

Output:

does_exist does_not_exist
8 NULL

Backlinks:

list from [[SQL - SQL Server - Check if a Column is Accessible]] AND -"Changelog"