Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to describe table in SQL Server 2008?

I want to describe a table in SQL Server 2008 like what we can do with the DESC command in Oracle.

I have table [EX].[dbo].[EMP_MAST] which I want to describe, but it does not work.

Error shown:

The object 'EMP_MAST' does not exist in database 'master' or is invalid for this operation.

like image 581
Niks Avatar asked Jun 18 '12 06:06

Niks


People also ask

How do you describe a table in SQL Server?

Just select table and press Alt + F1 , it will show all the information about table like Column name, datatype, keys etc.

How do I find the description of a table in SQL?

In Object Explorer, select the table for which you want to show properties. Right-click the table and choose Properties from the shortcut menu. For more information, see Table Properties - SSMS.

How do I Desc a table in SSMS?

If we are using the SSMS, the following steps are used to describe the table definition: Step 1: Connect to the database and navigate to the Object Explorer. Step 2: Select the table for which you want to display the properties.

How do you DESC a table?

[DESCRIBE | DESC] TABLE{name}[ TYPE = (STAGE | COLUMNS) ]; Here, the {name} defines an identifier for the particular table mentioned to describe it. We can enclose the whole string using double quotes which are case-sensitive when the identifier includes spaces or special characters.


2 Answers

You can use sp_columns, a system stored procedure for describing a table.

exec sp_columns TableName 

You can also use sp_help.

like image 95
Jainendra Avatar answered Oct 08 '22 11:10

Jainendra


According to this documentation:

DESC MY_TABLE

is equivalent to

SELECT column_name "Name", nullable "Null?", concat(concat(concat(data_type,'('),data_length),')') "Type" FROM user_tab_columns WHERE table_name='TABLE_NAME_TO_DESCRIBE';

I've roughly translated that to the SQL Server equivalent for you - just make sure you're running it on the EX database.

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' 
like image 25
Bridge Avatar answered Oct 08 '22 11:10

Bridge