我有q查询列出表中的列,如下所示
select name from sys.all_columns as AllCols where object_id=xxxxxx
我想显示除foriegn key
列以外的列有没有办法过滤上面的查询,以便它显示除外键列之外的所有列
答案 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