日期:2014-05-17 浏览次数:20853 次
SELECT DISTINCT 字段序号=a.colorder , 字段名=a.name , 外键字段所在的表=CASE WHEN tony.fkey is not null and tony.fkey=a.colid THEN object_name(tony.rkeyid) ELSE '' END , 外键字段=CASE WHEN tony.fkey is not null and tony.fkey=a.colid THEN (SELECT name FROM syscolumns WHERE colid=tony.fkey AND id=tony.fkeyid) ELSE '' END, 类型=b.name FROM sys.syscolumns a LEFT JOIN systypes b ON a.xtype = b.xusertype INNER JOIN sysobjects d ON a.id = d.id AND d.xtype = 'U' AND d.status >= 0 LEFT JOIN sysobjects htl ON htl.parent_obj=d.id AND htl.xtype='F' LEFT JOIN sysforeignkeys tony on htl.id=tony.constid WHERE d.name='Articles' --这里输入包含表名称的条件
SELECT 字段序号=a.column_id , 字段名=a.name , 外键字段所在的表=OBJECT_NAME(referenced_object_id), 外键字段=SC.name, SC.name, 类型=b.name FROM sys.columns a LEFT JOIN systypes b ON a.user_type_id = b.xusertype LEFT JOIN sys.foreign_key_columns FK ON FK.parent_object_id = a.object_id AND FK.parent_column_id = a.column_id LEFT JOIN sys.columns SC ON FK.referenced_object_id = SC.object_id AND FK.referenced_column_id = SC.column_id WHERE a.object_id=OBJECT_ID('tb') --这里输入包含表名称的条件
------解决方案--------------------
如果你是05 或刚高版本就不要在使用
syscolumns systypes sysobjects 了
请用
sys.columns sys.types sys.objects
------解决方案--------------------
select 字段序号,字段名,max(外键字段所在的表) as 外键字段所在的表,max(外键字段) as 外键字段,类型
from (SELECT distinct
字段序号=a.colorder ,
字段名=a.name ,
外键字段所在的表=CASE WHEN tony.fkey is not null and tony.fkey=a.colid
THEN object_name(tony.rkeyid) ELSE ''
END ,
外键字段=CASE WHEN tony.fkey is not null and tony.fkey=a.colid
THEN (SELECT name FROM syscolumns
WHERE colid=tony.fkey AND id=tony.fkeyid)
ELSE ''
END,
类型=b.name
FROM sys.syscolumns a
LEFT JOIN systypes b ON a.xtype = b.xusertype
INNER JOIN sysobjects d ON a.id = d.id AND d.xtype = 'U' AND d.status >= 0
LEFT JOIN sysobjects htl ON htl.parent_obj=d.id AND htl.xtype='F'
LEFT JOIN sysforeignkeys tony on htl.id=tony.constid
WHERE d.name='TB_Details' ) as TB
group by 字段序号,字段名,类型
字段不存在 ,不能啊 。测试通过的