为什么schema_name(schema_id)在sql server中的sys.types中给出null

时间:2015-07-19 05:15:09

标签: sql-server-2008

我想获取所有列名称的列表' xml'以及它们的表名和模式名称。我尝试了以下sql代码:

char next = 0;
while (next != '\n')
{
    scanf("%c", &next);
    if (next == ' ')
        spaceCount++;
}

除了显示为null的模式名称外,一切都很好。如果我更换

select 
    OBJECT_NAME(b.object_id) as t1,
    b.name, a.name,
    OBJECT_NAME(a.schema_id, DB_ID('AdventureWorks2008R2')) as schema 
from 
    sys.types as a
inner join 
    sys.columns as b on a.user_type_id = b.user_type_id
where 
    a.name = 'xml'

OBJECT_NAME(a.schema_id,DB_ID('AdventureWorks2008R2')) 

我仍然获得空值

3 个答案:

答案 0 :(得分:1)

您需要从sys.tables获取架构。 sys.types中的schema列指的是类型本身的模式。 试试这个:

select 
  OBJECT_NAME(b.object_id) as t1,
 b.name, 
 a.name,
SCHEMA_NAME(t.schema_id)
from 
  sys.types as a
inner join 
  sys.columns as b on a.user_type_id = b.user_type_id
inner join sys.tables t on t.object_id = b.object_id
where 
   a.name = 'xml'

答案 1 :(得分:1)

答案 2 :(得分:0)

尝试使用SCHEMA_NAME代替OBJECT_NAME

select 
    OBJECT_NAME(b.object_id) as t1,
    b.name, a.name,
    SCHEMA_NAME(a.schema_id)     -- use SCHEMA_NAME here
from 
    sys.types as a
inner join 
    sys.columns as b on a.user_type_id = b.user_type_id
where 
    a.name = 'xml'