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:
- TABLE_QUALIFIER: the name of the database that contains the table.
- TABLE_OWNER: the schema or owner of the table.
- TABLE_NAME: table name.
- COLUMN_NAME: column name.
- DATA_TYPE: SQL data type of the column.
- TYPE_NAME: name of the data type, for example int or varchar.
- PRECISION: precision of column, that is the total number of digits.
- LENGTH: length in bytes.
- SCALE: scale of column, that is the number of digits to right of decimal point
- RADIX: base of numeric data types; almost always 10 for integer and decimal types.
- 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.
- COLUMN_DEF: The default value for the column, if any.
- SQL_DATA_TYPE: SQL data type code.
- SQL_DATETIME_SUB: Subtype code for datetime and interval types.
- CHAR_OCTET_LENGTH: Maximum length in octets for character data types.
- ORDINAL_POSITION: The place of the column in the table, counting from 1.
- 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.
Similar Reads
Rename Column in SQL Server
SQL Server is a widely used Relational Database Management System (RDBMS) that allows users to create and manage databases effectively. Renaming a column in a database is a common task usually required when users want to change the database schema. In this article, we will explore different methods
3 min read
Rename column SQL Server 2008
Renaming a column in a database is a common task that often arises when users want to change the database schema. In SQL Server 2008, renaming columns can be done efficiently using the sp_rename system-stored procedure, as the ALTER TABLE RENAME COLUMN syntax is not supported in this version. This a
4 min read
SQL Server ALIASES
Aliases in SQL Server are the temporary names given to tables or columns to make it easy to read and maintain the data. Aliases help you to provide different names to columns and tables temporarily so that users can easily understand the data of the table and it does not change any data of the table
3 min read
PostgreSQL - Generate Columns
When working with databases, there are scenarios where you need a columnâs value to be automatically computed based on other columns. In PostgreSQL, this can be achieved through generated columns. These special columns are calculated based on an expression using other columns in the table. The value
3 min read
SQL Server PIVOT
SQL Server relational database management system. It has core functions that create, manipulate, and store data very efficiently. SQL Server contains all these characteristics and it has an extremely user-friendly installation interface, unlike other database servers that require extensive command-l
7 min read
Deleting a Column in SQL Server
Structure Query Language (SQL) is a standard language to manipulate and manage the database. SQL is a very powerful language for managing the database. SQL Server Delete command is one of the SQL commands that is used to remove the data that is not useful or due to which inconsistency occurred in th
5 min read
Dynamic SQL in SQL Server
In SQL Server, at times the SQL Queries need to be dynamic and not static, meaning the complete SQL query may be built dynamically at run time as a string using the user inputs and any specific application logic. This can be done in queries run from back-end applications or inside stored procedures.
6 min read
SQL Server Group Functions
The group function in SQL Server provides a powerful tool for performing calculations on groups of rows, allowing you to group data based on specific criteria. This function is important when you want to analyze and summarize information from multiple records in a data structure. The basic group fun
3 min read
SQLite Rename Column
Renaming a Column becomes necessary when there is a certain change that appears to be visible in the column the name given to the column previously is vague or it doesn't represent what the column holds exactly. SQLite provides the modified version of the ALTER TABLE command which lets the user rena
6 min read
SQL Server ALTER TABLE DROP COLUMN
In SQL Server, there could be some situations when we may have to delete or drop a column from a table. Sometimes the column in a table was created wrongly or maybe it is no longer required and has become obsolete. So, to drop a column from a table, the ALTER TABLE, DROP COLUMN SQL query is used. In
4 min read