我通常使用以下sql
查找sql server中表的所有列名SELECT Upper(column_name) FROM information_schema.columns
WHERE table_name ='mutable'
但是,现在我想找到同义词的所有列名。有什么方法可以做到这一点?
答案 0 :(得分:3)
恕我直言you shouldn't use INFORMATION_SCHEMA
for any of this。以下内容适用于表格或视图:
SELECT UPPER(name)
FROM sys.columns
WHERE OBJECT_NAME([object_id]) = N'mutable';
以下内容适用于同义词(您稍后更改的要求):
SELECT UPPER(name)
FROM sys.columns AS c
CROSS APPLY
(
SELECT name
FROM sys.synonyms
WHERE name = N'mutable'
AND OBJECT_ID([base_object_name]) = c.[object_id]
) AS x;
你也可以说:
DECLARE @obj INT;
SELECT @obj = OBJECT_ID([base_object_name])
FROM sys.synonyms WHERE name = N'mutable';
SELECT UPPER(name) FROM sys.columns WHERE [object_id] = @obj;
答案 1 :(得分:3)
您可以使用sys.synonyms
来发现base_object_name
,并在那里使用sys.columns
,如@Aaron所示:
select * from sys.columns where
object_id = OBJECT_ID((select base_object_name from sys.synonyms
where name='mutable'))
答案 2 :(得分:2)
尝试从同义词中选择前面的零行到临时表中。这将创建一个与基础对象具有相同列名的表。然后在tempdb中查询sys.columns:
completionHandler