如何获取外键属于表

时间:2013-11-20 09:30:04

标签: sql-server-2008

我有q查询列出表中的列,如下所示

select name from sys.all_columns as AllCols where object_id=xxxxxx

我想显示除foriegn key列以外的列有没有办法过滤上面的查询,以便它显示除外键列之外的所有列

1 个答案:

答案 0 :(得分:0)

您可以通过查询sys.sysforeignkeys,然后使用EXCEPT运算符从'AllCols'查询中删除结果来实现。

DECLARE @objectid INT = object_id('Schema.TableName')

SELECT name  FROM sys.all_columns as AllCols
WHERE object_id=@Objectid
EXCEPT
SELECT name  FROM sys.all_columns as AllCols
INNER JOIN sys.sysforeignkeys AS AllFks
ONAllCols.column_id = AllFks.fkey or AllCols.column_id = AllFks.rkey
WHERE object_id=@Objectid