Keywords: SQL Server 2008 | Describe Table | INFORMATION_SCHEMA
Abstract: This article explores methods to emulate the Oracle DESC command in SQL Server 2008. It provides a detailed SQL query using the INFORMATION_SCHEMA.Columns system view to retrieve metadata such as column names, nullability, and data types. The piece compares alternative approaches like sp_columns and sp_help, explains the cause of common errors, and offers guidance for cross-database queries. Covering data type formatting, length handling, and practical applications, it serves as a valuable resource for database developers and administrators.
Problem Background and Error Analysis
In Oracle databases, the DESC command is commonly used to quickly view table structures, including column names, data types, and nullability. However, in SQL Server 2008, attempting a similar command often results in errors. For instance, when a user tries to describe the table [EX].[dbo].[EMP_MAST], the system may return: "The object 'EMP_MAST' does not exist in database 'master' or is invalid for this operation." This error typically occurs due to an incorrect database context, as SQL Server defaults to searching in the master database, while the target table resides in the EX database.
Core Solution: Utilizing INFORMATION_SCHEMA.Columns
Based on best practices, SQL Server offers the INFORMATION_SCHEMA.Columns system view to simulate Oracle's DESC functionality. Below is a complete query example that returns column names, nullability flags, and formatted data types:
SELECT column_name AS [name],
IS_NULLABLE AS [null?],
DATA_TYPE + COALESCE('(' + CASE WHEN CHARACTER_MAXIMUM_LENGTH = -1
THEN 'Max'
ELSE CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(5))
END + ')', '') AS [type]
FROM INFORMATION_SCHEMA.Columns
WHERE table_name = 'EMP_MAST'This query filters columns from the INFORMATION_SCHEMA.Columns view where the table name is EMP_MAST. Key components include:
column_namedisplays the name of the column.IS_NULLABLEindicates whether the column allows null values, returning 'YES' or 'NO'.- The data type is constructed by concatenating
DATA_TYPEwith an optional character maximum length. WhenCHARACTER_MAXIMUM_LENGTHis -1, it denotes a maximum length (e.g.,varchar(max)), otherwise, it shows the specific value.COALESCEhandles cases with no length (e.g., numeric types).
Before executing this query, ensure the database context is correct. If the table is in the EX database, use USE EX; to switch contexts, or fully qualify the table name. For example, modify the WHERE clause to WHERE table_catalog = 'EX' AND table_schema = 'dbo' AND table_name = 'EMP_MAST' for enhanced accuracy.
Comparison of Alternative Methods
In addition to custom queries, SQL Server provides built-in stored procedures as supplementary options:
sp_columns: ExecutingEXEC sp_columns 'EMP_MAST'returns detailed column information, including data types, lengths, and nullability, but the output is comprehensive and may include extra metadata.sp_help: RunningEXEC sp_help '[EX].[dbo].[EMP_MAST]'offers a broad overview of the table, such as columns, indexes, and constraints, but the output structure is complex and not ideal for quick inspections.
Compared to the INFORMATION_SCHEMA query, these stored procedures are easier to use but allow less control over formatting. For instance, sp_help output may include multiple result sections, whereas INFORMATION_SCHEMA focuses directly on column metadata.
Practical Applications and Considerations
In real-world scenarios, this method is useful for database migration, documentation generation, or debugging. For example, when migrating from Oracle to SQL Server, this query can help verify table structure consistency. Key considerations include:
- Ensure database permissions allow access to system views.
- For special data types, extend the query to include precision and scale (e.g., for
decimaltypes). - In cross-database queries, use fully qualified names to avoid context errors.
In summary, by leveraging the INFORMATION_SCHEMA.Columns view, SQL Server users can effectively replace Oracle's DESC command, improving database management efficiency.