如何在 SQL Server 2008 中检查用户定义表类型是否存在?

问题描述:

我有一个用户定义的表类型.我想在使用 OBJECT_ID(name, type) 函数编辑补丁之前检查它是否存在.

I have a user-defined table type. I want to check it's existence before editing in a patch using OBJECT_ID(name, type) function.

来自枚举type应该为用户定义的表类型传递吗?

What type from the enumeration should be passed for user-defined table types?

N'U' 像用户定义的表一样不起作用,即 IF OBJECT_ID(N'MyType', N'U') IS NOT NULL>

N'U' like for user defined table doesn't work, i.e. IF OBJECT_ID(N'MyType', N'U') IS NOT NULL

可以在sys.types中查看或者使用TYPE_ID:

You can look in sys.types or use TYPE_ID:

IF TYPE_ID(N'MyType') IS NULL ...

只是一个预防措施:使用 type_id 不会验证类型是 table 类型——只是存在具有该名称的类型.否则 gbn 的查询可能会更好.

Just a precaution: using type_id won't verify that the type is a table type--just that a type by that name exists. Otherwise gbn's query is probably better.