我有一个SQL Server数据库,我想知道它有什么列和类型。我更喜欢通过查询而不是使用像Enterprise Manager这样的GUI来实现这一点。有办法做到这一点吗?


当前回答

请使用以下sql查询;这对我的案子很管用。

select * FROM   INFORMATION_SCHEMA.Columns where table_name = 'tablename';

其他回答

首先连接到你的数据库,

使用DB_name

然后

exec sp_help '生产。Et_Issue”

这里的'production'是模式名。如果你没有图式, 您可以简单地写入sp_help table_name

选择表格,按Alt+F1,

它将显示关于表的所有信息,如列名,数据类型,键等。

SQL Server中与Oracle的describe命令等价的是存储的proc sp_help

describe命令提供了列名、类型、长度等信息。

在SQL Server中,假设你想在数据库'mydb'中的模式'myschema'中描述一个表'mytable',你可以这样做:

USE mydb;
exec sp_help 'myschema.mytable';

如果你正在使用Brent Ozar团队的FirstResponderKit,你也可以运行这个查询:

exec sp_blitzindex @tablename='MyTable'

它将返回关于表的所有信息:

索引及其使用统计信息(读、写、锁等)、空间 二手和其他 缺失索引 列 外键 统计内容

当然,它不是一个系统,也不像sp_help或sp_columns那样通用,但它返回关于表的所有可能信息,我认为值得在您的环境中创建它并在这里提到它。

以防您不想使用stored proc,这里有一个简单的查询版本

select * 
  from information_schema.columns 
 where table_name = 'aspnet_Membership'
 order by ordinal_position