Equivalent Methods for Describing Table Structures in SQL Server 2008: Transitioning from Oracle DESC to INFORMATION_SCHEMA

Nov 23, 2025 · Programming · 9 views · 7.8

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:

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:

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:

In summary, by leveraging the INFORMATION_SCHEMA.Columns view, SQL Server users can effectively replace Oracle's DESC command, improving database management efficiency.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.