1.获取数据库表信息的SQL语句
Code
select * from sysobjects where xtype = 'U' and name <> 'sysdiagrams'
select * from sysobjects where xtype = 'U' and name <> 'sysdiagrams'
运行结果:
2.获取数据表列信息的SQL语句
Code
declare @table_name as varchar(max);
set @table_name = 'Franchiser';
--获取当前表的主键。
EXECUTE sp_helpindex @table_name;
--获取当前表的列信息。
SELECT
syscolumns.name as Name,
systypes.name as Type,
syscolumns.length as Length,
syscolumns.isnullable as IsNullAble,
sys.columns.is_identity as IsIdentity,
sys.extended_properties.value as Description
FROM
syscolumns,sys.columns,sys.extended_properties,systypes
WHERE
syscolumns.id = object_id (@table_name)
AND sys.columns.column_id=syscolumns.colid
AND sys.columns.object_id=syscolumns.id
AND sys.extended_properties.major_id=sys.columns.object_id
AND sys.extended_properties.minor_id=sys.columns.column_id
AND sys.extended_properties.name='MS_Description'
AND syscolumns.xusertype = systypes.xusertype;
--获取当前表所有的主外键约束。
SELECT
OBJECT_NAME(F.constid) as Name,
(IST.TABLE_SCHEMA + '.' + O.name) AS FKeyTable,
(IST2.TABLE_SCHEMA + '.' + O2.name) AS RKeyTable,
C.name AS FKeyColumn,
C2.name AS RKeyColumn
FROM
sysforeignkeys F
JOIN sysobjects O ON O.id=fkeyid
JOIN sysobjects O2 ON O2.id=f.rkeyid
JOIN syscolumns C ON C.id=F.fkeyid AND C.colid=F.fkey
JOIN syscolumns C2 ON C2.id=F.rkeyid AND C2.colid=F.rkey
LEFT JOIN INFORMATION_SCHEMA.TABLES IST ON OBJECT_ID(IST.TABLE_SCHEMA + '.' + IST.TABLE_NAME) = O.ID
LEFT JOIN INFORMATION_SCHEMA.TABLES IST2 ON OBJECT_ID(IST2.TABLE_SCHEMA + '.' + IST2.TABLE_NAME) = O2.ID
WHERE F.rkeyid=object_id(@table_name) OR F.fkeyid=object_id(@table_name);
DECLARE @colname sysname;
SELECT @colname=name FROM syscolumns WHERE (id = OBJECT_ID(@table_name)) AND (colstat & 1 = 1);
--获取当前表的自增列。
SELECT 'Identity'= ISNULL(@colname,'No identity column defined.'),'Seed'= ident_seed(@table_name),'Increment'= ident_incr(@table_name);
declare @table_name as varchar(max);
set @table_name = 'Franchiser';
--获取当前表的主键。
EXECUTE sp_helpindex @table_name;
--获取当前表的列信息。
SELECT
syscolumns.name as Name,
systypes.name as Type,
syscolumns.length as Length,
syscolumns.isnullable as IsNullAble,
sys.columns.is_identity as IsIdentity,
sys.extended_properties.value as Description
FROM
syscolumns,sys.columns,sys.extended_properties,systypes
WHERE
syscolumns.id = object_id (@table_name)
AND sys.columns.column_id=syscolumns.colid
AND sys.columns.object_id=syscolumns.id
AND sys.extended_properties.major_id=sys.columns.object_id
AND sys.extended_properties.minor_id=sys.columns.column_id
AND sys.extended_properties.name='MS_Description'
AND syscolumns.xusertype = systypes.xusertype;
--获取当前表所有的主外键约束。
SELECT
OBJECT_NAME(F.constid) as Name,
(IST.TABLE_SCHEMA + '.' + O.name) AS FKeyTable,
(IST2.TABLE_SCHEMA + '.' + O2.name) AS RKeyTable,
C.name AS FKeyColumn,
C2.name AS RKeyColumn
FROM
sysforeignkeys F
JOIN sysobjects O ON O.id=fkeyid
JOIN sysobjects O2 ON O2.id=f.rkeyid
JOIN syscolumns C ON C.id=F.fkeyid AND C.colid=F.fkey
JOIN syscolumns C2 ON C2.id=F.rkeyid AND C2.colid=F.rkey
LEFT JOIN INFORMATION_SCHEMA.TABLES IST ON OBJECT_ID(IST.TABLE_SCHEMA + '.' + IST.TABLE_NAME) = O.ID
LEFT JOIN INFORMATION_SCHEMA.TABLES IST2 ON OBJECT_ID(IST2.TABLE_SCHEMA + '.' + IST2.TABLE_NAME) = O2.ID
WHERE F.rkeyid=object_id(@table_name) OR F.fkeyid=object_id(@table_name);
DECLARE @colname sysname;
SELECT @colname=name FROM syscolumns WHERE (id = OBJECT_ID(@table_name)) AND (colstat & 1 = 1);
--获取当前表的自增列。
SELECT 'Identity'= ISNULL(@colname,'No identity column defined.'),'Seed'= ident_seed(@table_name),'Increment'= ident_incr(@table_name);
查询结果:
三、SQL 2000/2005/2008通用查询
Code
SELECT
Sysobjects.name AS [表名],
syscolumns.name AS [列名],
systypes.name AS [数据类型],
syscolumns.length AS [字段长度],
CASE
WHEN systypes.name
IN ('nvarchar','nchar','ntext')
THEN
CASE WHEN syscolumns.length / 2 > 0
THEN syscolumns.length / 2
ELSE -1
END
ELSE syscolumns.length
END AS [实际长度],
sys.extended_properties.[value] AS [字段描述],
syscomments.text AS [默认值],
syscolumns.isnullable AS [是否允许空值],
ISNULL(COLUMNPROPERTY(syscolumns.id, syscolumns.name, 'Scale'), 0) AS [小数位数],
COLUMNPROPERTY(syscolumns.id, syscolumns.name, 'IsIdentity') AS [自增标识] ,
CASE WHEN COLUMNPROPERTY(syscolumns.id, syscolumns.name, 'IsIdentity') = 1
THEN
ident_seed(Sysobjects.name)
ELSE
0
END AS 初始标识,
CASE WHEN COLUMNPROPERTY(syscolumns.id, syscolumns.name, 'IsIdentity') = 1
THEN
ident_incr(Sysobjects.name)
ELSE
0
END AS 增长值,
CASE WHEN EXISTS
(
SELECT 1
FROM sysobjects
WHERE xtype = 'PK' AND name IN
(
SELECT name
FROM sysindexes
WHERE indid IN
(
SELECT indid
FROM sysindexkeys
WHERE id = syscolumns.id AND colid = syscolumns.colid
)
)
) THEN 1 ELSE 0 END AS [主键]
FROM syscolumns
INNER JOIN systypes
ON syscolumns.xtype = systypes.xtype
LEFT JOIN sysobjects ON syscolumns.id = sysobjects.id
LEFT OUTER JOIN sys.extended_properties ON
(
sys.extended_properties.minor_id = syscolumns.colid
AND sys.extended_properties.name='MS_Description'
AND sys.extended_properties.major_id = syscolumns.id
)
LEFT OUTER JOIN syscomments ON syscolumns.cdefault = syscomments.id
WHERE (systypes.name <> 'sysname')
AND syscolumns.id IN (SELECT id FROM SYSOBJECTS WHERE xtype = 'U') --where可通过name读取单表
ORDER BY [表名],[列名];
SELECT
Sysobjects.name AS [表名],
syscolumns.name AS [列名],
systypes.name AS [数据类型],
syscolumns.length AS [字段长度],
CASE
WHEN systypes.name
IN ('nvarchar','nchar','ntext')
THEN
CASE WHEN syscolumns.length / 2 > 0
THEN syscolumns.length / 2
ELSE -1
END
ELSE syscolumns.length
END AS [实际长度],
sys.extended_properties.[value] AS [字段描述],
syscomments.text AS [默认值],
syscolumns.isnullable AS [是否允许空值],
ISNULL(COLUMNPROPERTY(syscolumns.id, syscolumns.name, 'Scale'), 0) AS [小数位数],
COLUMNPROPERTY(syscolumns.id, syscolumns.name, 'IsIdentity') AS [自增标识] ,
CASE WHEN COLUMNPROPERTY(syscolumns.id, syscolumns.name, 'IsIdentity') = 1
THEN
ident_seed(Sysobjects.name)
ELSE
0
END AS 初始标识,
CASE WHEN COLUMNPROPERTY(syscolumns.id, syscolumns.name, 'IsIdentity') = 1
THEN
ident_incr(Sysobjects.name)
ELSE
0
END AS 增长值,
CASE WHEN EXISTS
(
SELECT 1
FROM sysobjects
WHERE xtype = 'PK' AND name IN
(
SELECT name
FROM sysindexes
WHERE indid IN
(
SELECT indid
FROM sysindexkeys
WHERE id = syscolumns.id AND colid = syscolumns.colid
)
)
) THEN 1 ELSE 0 END AS [主键]
FROM syscolumns
INNER JOIN systypes
ON syscolumns.xtype = systypes.xtype
LEFT JOIN sysobjects ON syscolumns.id = sysobjects.id
LEFT OUTER JOIN sys.extended_properties ON
(
sys.extended_properties.minor_id = syscolumns.colid
AND sys.extended_properties.name='MS_Description'
AND sys.extended_properties.major_id = syscolumns.id
)
LEFT OUTER JOIN syscomments ON syscolumns.cdefault = syscomments.id
WHERE (systypes.name <> 'sysname')
AND syscolumns.id IN (SELECT id FROM SYSOBJECTS WHERE xtype = 'U') --where可通过name读取单表
ORDER BY [表名],[列名];