简体   繁体   English

SQL Server创建用户定义的表类型与架构不能正常工作

[英]SQL Server create User-Defined Table Types with schema not working properly

I'm creating User-Defined Table Types on SQL Server 2008R2/2012 with this statement: 我正在使用以下语句在SQL Server 2008R2 / 2012上创建用户定义的表类型:

CREATE TYPE [MySchemaName].[MyUserTableType] 
   As Table ( [Id] varchar(20) NOT NULL );

This works as intended. 这按预期工作。

I also got a stored procedure which deletes all associated object to a given Schema. 我还得到了一个存储过程,该过程将所有关联的对象删除到给定的架构。 This is where my User-Defined Table Types are not working properly. 这是我的用户定义表类型无法正常工作的地方。 I am expecting that those Types are created with an associated schema. 我希望这些类型是用关联的架构创建的。

If i try to drop the schema, it will complain that the schema is associated with my User-Defined Table Type. 如果我尝试删除该架构,它将抱怨该架构与我的用户定义表类型相关联。 Hence it will not be deleted. 因此,它将不会被删除。

But if i query all objects in my DB which are TYPE_TABLE it tells me that my Table types do not belong to my schema. 但是,如果我查询数据库中所有属于TYPE_TABLE的对象,它将告诉我表类型不属于我的模式。 They belong to the schema 'sys' 它们属于架构'sys'

SELECT name, schema_id, type_desc 
FROM [TESTDB].SYS.OBJECTS 
WHERE type_desc = 'TYPE_TABLE';

SELECT * FROM sys.schemas;

Any idea why? 知道为什么吗?

Here is a screenshot from my output 这是我的输出的屏幕截图 在此处输入图片说明

Instead of looking in sys.objects for these you should look in sys.types or sys.table_types (which additionally exposes the type_table_object_id ). 而不是在sys.objects查找这些内容,而应在sys.typessys.table_types (它们另外公开了type_table_object_id )。

SELECT name,
       schema_id /*Will be the "test" schema id*/
FROM   sys.types
WHERE  is_table_type = 1
       AND name = 'MyUserTableType'

When you create a user defined type it adds a row to sys.sysscalartypes with the user supplied schema and name and a row to sys.sysschobjs with a system generated name in the sys schema. 当你创建一个用户定义类型它增加了一个行sys.sysscalartypes与用户提供的模式和名称和Row sys.sysschobjs同在一个系统生成的名称sys架构。 The system generated name is created by concatenating TT_ + FriendlyName + _ + Hex version of object id. 通过串联TT_ + FriendlyName + _ +对象ID的十六进制版本来创建系统生成的名称。

The two entities are related together via sys.syssingleobjrefs 这两个实体通过sys.syssingleobjrefs关联在一起

/*This query only works via the DAC*/
SELECT so.id AS object_id,
       st.id AS user_type_id,
       *
FROM   sys.sysschobjs so
       JOIN sys.syssingleobjrefs sor
         ON sor.indepid = so.id
       JOIN sys.sysscalartypes st
         ON st.id = sor.depid
WHERE  st.name = 'MyUserTableType' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM