Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show the table structure in SQL Server query?

Tags:

sql-server

SELECT DateTime, Skill, Name, TimeZone, ID, User, Employee, Leader 
FROM t_Agent_Skill_Group_Half_Hour AS t

I need to view the table structure in a query.

like image 918
Bassam Qarib Avatar asked Aug 18 '13 11:08

Bassam Qarib


People also ask

How do you display the structure of a table?

- The structure of a table can be viewed using the DESCRIBE TABLE_NAME command. - Provides a description of the specified table or view. For a list of tables in the current schema, use the Show Tables command. - For a list of views in the current schema, use the Show Views command.

How can I see the structure of a SQL Server view?

Get view properties by using Object ExplorerIn Object Explorer, select the plus sign next to the database that contains the view to which you want to view the properties, and then click the plus sign to expand the Views folder. Right-click the view of which you want to view the properties and select Properties.

How do you find the structure of a database table?

To show the schema, we can use the DESC command. This gives the description about the table structure.


2 Answers

For SQL Server, if using a newer version, you can use

select *
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='tableName'

There are different ways to get the schema. Using ADO.NET, you can use the schema methods. Use the DbConnection's GetSchema method or the DataReader'sGetSchemaTable method.

Provided that you have a reader for the for the query, you can do something like this:

using(DbCommand cmd = ...)
using(var reader = cmd.ExecuteReader())
{
    var schema = reader.GetSchemaTable();
    foreach(DataRow row in schema.Rows)
    {
        Debug.WriteLine(row["ColumnName"] + " - " + row["DataTypeName"])
    }
}

See this article for further details.

like image 91
PHeiberg Avatar answered Sep 22 '22 02:09

PHeiberg


sp_help tablename in sql server

desc tablename in oracle

like image 57
Vinoth_S Avatar answered Sep 21 '22 02:09

Vinoth_S