Open In App

sp_columns - SQL Server

Last Updated : 21 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In SQL Server, managing and understanding database schemas is crucial for effective database administration and development. The sp_columns stored procedure is a valuable tool for retrieving detailed metadata about the columns of a specified table or view. In this article, We will learn about sp_columns in SQL Server in detail.

sp_columns in SQL Server

The sp_columns stored procedure in SQL Server is used to retrieve detailed metadata information about the columns of a specified table or view. It provides details such as column names, data types, sizes, nullability, and default values. This information is useful for database administrators and developers who need to understand or document the schema of a database, build dynamic SQL queries, or perform other tasks that require detailed column information

Syntax:

sp_columns [ @table_name = ] object
[ , [ @table_owner = ] owner ]
[ , [ @table_qualifier = ] qualifier ]
[ , [ @column_name = ] column ]

Parameters

  • @table_name: The named table or view whose column information is required. This is a required parameter.
  • @table_owner: The owner of the table or view.
  • @table_qualifier: Specifies the name of the database that contains the table or view. This is optional.
  • @column_name: The name of the particular column whose information is requested. This is optional.

Arguments

The sp_columns stored procedure takes several optional arguments:

sp_columns [ @table_name = ] 'table_name' 
[ , [ @table_owner = ] 'table_owner' ]
[ , [ @table_qualifier = ] 'table_qualifier' ]
[ , [ @column_name = ] 'column_name' ]
[ , [ @ODBCVer = ] 'ODBCVer' ]
[ , [ @fUsePatterns = ] 'fUsePatterns' ]

Explanation:

  • @table_name : The name of the table or view for which column information has to be returned. This is a mandatory parameter.
  • @table_owner: The owner of the table. If the object is omitted then it is assumed that the current user is acting upon it. If specified as % then it returns the columns for all tables that have columns with the same name in all schemas.
  • @table_qualifier: The name of the database in which the table is located. If not specified, the current database is assumed.
  • @column_name: The name of the column to filter by. If not supplied, information on all columns is returned.
  • @ODBCVer: Sets the ODBC version. Normally, 2 or 3, which modifies the form of the output.
  • @fUsePatterns If 1, enables the use of wildcard patterns (% and _) for table_name and column_name. Default value is 0.

Return Code Values

The sp_columns stored procedure returns a value indicating the success or failure of the procedure:

  • 0: Success.
  • 1: Failure; for example, the specified table or column does not exist.

Result Set

The result set returned by sp_columns contains detailed information for each column of the specified table or view. These columns may be used in the result set:

  1. TABLE_QUALIFIER: the name of the database that contains the table.
  2. TABLE_OWNER: the schema or owner of the table.
  3. TABLE_NAME: table name.
  4. COLUMN_NAME: column name.
  5. DATA_TYPE: SQL data type of the column.
  6. TYPE_NAME: name of the data type, for example int or varchar.
  7. PRECISION: precision of column, that is the total number of digits.
  8. LENGTH: length in bytes.
  9. SCALE: scale of column, that is the number of digits to right of decimal point
  10. RADIX: base of numeric data types; almost always 10 for integer and decimal types.
  11. NULLABLE: Indicates whether the column may include NULL values. A value of 1 indicates that NULL is possible, while a value of 0 indicates it is disallowed. REMARKS: Any remarks or comments concerning the column.
  12. COLUMN_DEF: The default value for the column, if any.
  13. SQL_DATA_TYPE: SQL data type code.
  14. SQL_DATETIME_SUB: Subtype code for datetime and interval types.
  15. CHAR_OCTET_LENGTH: Maximum length in octets for character data types.
  16. ORDINAL_POSITION: The place of the column in the table, counting from 1.
  17. IS_NULLABLE: A flag to indicate if the column is nullable. YES or NO.

Permissions

  • Execute Permission: User must have the SELECT permission to the schema containing the table or view against which the query is executed.
  • Database Visibility: User should also be granted to view metadata of target table, normally it means by having VIEW DEFINITION permission.

Remarks

  • Usage: The sp_columns is normally used in Database Management scripts to dynamically read column metadata, which will be applied in several types of operations, including being dynamically constructed in queries or generating documentation.
  • Cross-Database Queries: The sp_columns may query metadata across different databases if a proper @table_qualifier is provided and the end-user has permissions to do so.
  • ODBC Compatibility: This SP proves to be very useful for ODBC applications by returning the metadata in a form compatible with the ODBC standard.
  • Deprecation Note: Though sp_columns still survives, for more standardised metadata queries, INFORMATION_SCHEMA.COLUMNS view shall be used by the developers, mostly for new projects.

Examples

To use the sp_columns stored procedure, you will need to provide it with a table name argument for the table from which you wish to obtain column information. This section provides examples of basic usage.

Example 1: Retrieve Column Information for a Table

To get information about all columns in the Employee table:

EXEC sp_columns @table_name = 'Employee';

Output:

TABLE_QUALIFIER

TABLE_OWNER

TABLE_NAME

COLUMN_NAME

DATA_TYPE

TYPE_NAME

PRECISION

LENGTH

SCALE

RADIX

NULLABLE

REMARKS

COLUMN_DEF

SQL_DATA_TYPE

SQL_DATETIME_SUB

CHAR_OCTET_LENGTH

ORDINAL_POSITION

IS_NULLABLE

MyDatabase

dbo

Employee

EmployeeID

4

int

4

10

NULL

NULL

4

NULL

NULL

1

NO




MyDatabase

dbo

Employee

EmployeeID

12

varchar

50

50

NULL

NULL

1

NULL

NULL

12

NULL

NULL

50

YES

MyDatabase

dbo

Employee

EmployeeID

12

varchar

50

50

NULL

NULL

1

NULL

NULL

12

NULL

NULL

50

YES

MyDatabase

dbo

Employee

EmployeeID

91

datetime

NULL

8

NULL

NULL

1

NULL

NULL

91

NULL

NULL

NULL

YES

Example 2: Retrieve Column Information for a Table with a Specific Owner

EXEC sp_columns @table_name = 'Employee', @table_owner = 'dbo';

Output:

(Same as above, but filters results for tables owned by 'dbo')

Example 3: Retrieve Column Information for a Table in a Specific Database

EXEC sp_columns @table_name = 'Employee', @table_qualifier = 'HRDatabase';

Output:

(Same as above, but filters results for tables in the 'HRDatabase' database)

Conclusion

The sp_columns stored procedure in SQL Server is one of the options for retrieving detailed column metadata for any given table or view. It will be able to return column names, data types, and lengths; whether a column is nullable or not; and default values—all that is necessary for schema documentation, building dynamic SQL, or checking database integrity. It provides options for the table name, owner, database, and column name, among others, thereby facilitating fine-grained queries. This makes sp_columns a very useful tool for database administrators and developers in managing and analyzing SQL Server databases.


Next Article
Article Tags :

Similar Reads