用SQL语句获取MSSQL表关键字段的名称、数据类型和长度

时间:2022-08-30 14:26:46

用SQL语句获取MSSQL表关键字段的名称、数据类型和长度

SELECT syscolumns.name,systypes.name,syscolumns.isnullable,syscolumns.length FROM syscolumns,systypes,sysobjects,sysindexes,sysindexkeys WHERE syscolumns.xusertype = systypes.xusertype AND syscolumns.id = object_id('表名称') AND sysobjects.xtype = 'PK' AND sysobjects.parent_obj = syscolumns.id AND sysindexes.id = syscolumns.id AND sysobjects.name = sysindexes.name AND sysindexkeys.id = syscolumns.id AND sysindexkeys.indid = sysindexes.indid AND syscolumns.colid = sysindexkeys.colid

如果表有多个主键,那可返回多个主键信息。