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

Posted Chuck Lu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了How can I show the table structure in SQL Server query?相关的知识,希望对你有一定的参考价值。

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

回答1

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.

 

回答2

In SQL Server, you can use this query:

USE Database_name

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME=\'Table_Name\';

And do not forget to replace Database_name and Table_name with the exact names of your database and table names.

 

以上是关于How can I show the table structure in SQL Server query?的主要内容,如果未能解决你的问题,请参考以下文章

How can I manually create a authentication cookie instead of the default method?

When can I get the "java.net.SocketException: Permission denied:connect" message? And how

How can I list all foreign keys referencing a given table in SQL Server?

Git does not apply deleted files when merging an old branch into the master. How can I tell Git to a

第四百三十七天 how can I 坚持

[2016-03-16]How can I take better study notes?