逆水行船

别人的天堂,是我的异乡;无端的繁华,倍添我的惆怅

 

读取SQL-Server2005的系统数据

取得数据库中的表信息:
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 sys.sysobjects oj LEFT JOIN sys.extended_properties ep ON ep.major_id = oj.id AND ep.minor_id = 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 sys.syscolumns cl LEFT JOIN sys.sysobjects oj ON oj.id = cl.id LEFT JOIN sys.systypes tp ON cl.xtype = tp.xtype LEFT JOIN sys.syscomments cm ON cm.id=cl.cdefault LEFT JOIN sys.extended_properties ep ON ep.major_id = cl.id AND ep.minor_id = 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 sys.sysobjects oj INNER JOIN sys.sysforeignkeys fk ON oj.xtype = 'F' AND oj.id = fk.constid INNER JOIN sys.syscolumns cl ON fk.fkeyid = cl.id INNER JOIN sys.syscolumns RCol ON fk.rkeyid = RCol.id AND fk.fkey = cl.colid AND fk.rkey = RCol.colid


取得数据库表的主键信息:
存储过程:sp_pkeys  参数@table_name

posted on 2007-09-01 15:17  荣-  阅读(231)  评论(0编辑  收藏  举报

导航