取得数据库中的表信息
SELECT Script = CASE WHEN ep.[Value] IS NULL THEN oj.Name ELSE ep.[Value] END, oj.Name AS TableName, oj.id AS id, oj.crdate AS createDate,
oj.type AS tableType FROM dbo.sysobjects oj
LEFT JOIN dbo.sysproperties ep ON ep.type = oj.id AND ep.smallid = 0 ORDER BY oj.id
取得数据库中所有列信息的SQL语句:
SELECT oj.Name AS TableName, cl.Name AS ColumnName,tp.Name AS ColumnType,
tp.xtype AS ColumnTypeIndex, cl.length, cl.xscale AS decimaldigits,
Script = CASE WHEN ep.[Value] IS Null THEN cl.Name ELSE ep.[Value] END,
DefaultValue = CASE WHEN cm.text IS NULL THEN '' WHEN cm.text = '(getdate())' THEN '当前日期' ELSE cm.text END,
cl.isnullable, cl.colstat AS IsMarking, cl.id, cl.colid FROM dbo.syscolumns cl LEFT JOIN dbo.sysobjects oj ON oj.id = cl.id
LEFT JOIN dbo.systypes tp ON cl.xtype = tp.xtype LEFT JOIN dbo.syscomments cm ON cm.id=cl.cdefault
LEFT JOIN dbo.sysproperties ep ON ep.type = cl.id AND ep.smallid = cl.colid
WHERE oj.type='U' AND tp.Name != 'sysname' ORDER BY oj.id, cl.colid
取得数据库中的外键信息:
SELECT OBJECT_NAME(fk.constid) AS FKName, fk.constid AS FKID,
OBJECT_NAME(fk.fkeyid) AS TableName, fk.fkeyid AS TableID, cl.name AS ColumnName, RCol.name AS RColumnName,
OBJECT_NAME(fk.rkeyid) AS ParentTableName, fk.rkeyid AS ParentTableID FROM dbo.sysobjects oj INNER JOIN
dbo.sysforeignkeys fk ON oj.xtype = 'F' AND oj.id = fk.constid INNER JOIN dbo.syscolumns cl ON
fk.fkeyid = cl.id INNER JOIN dbo.syscolumns RCol ON fk.rkeyid = RCol.id AND fk.fkey = cl.colid AND fk.rkey = RCol.colid
取得数据库表的主键信息:
存储过程:sp_pkeys 参数@table_name