DEV Community

Cover image for Get the table structure in SQL
Ronak Munjapara
Ronak Munjapara Subscriber

Posted on

Get the table structure in SQL

I'll help you create queries to get the table structure (field names and data types) for these tables in the Academics database. Here's the query using INFORMATION_SCHEMA:

-- Get column details for Tablename 
SELECT 
    COLUMN_NAME,
    DATA_TYPE,
    CHARACTER_MAXIMUM_LENGTH,
    IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE 
    TABLE_NAME = 'Tablename'
    AND TABLE_SCHEMA = 'dbo'
ORDER BY ORDINAL_POSITION;

Enter fullscreen mode Exit fullscreen mode

Alternatively, you can use the system stored procedure sp_help:

-- Using sp_help (provides more detailed information including constraints)
EXEC sp_help 'Tablename';

Enter fullscreen mode Exit fullscreen mode

Both methods will give you the structure of these tables. The INFORMATION_SCHEMA query provides a cleaner, more focused view of just the column names and data types, while sp_help provides additional information like constraints, indexes, and other table properties.

Top comments (0)