简体   繁体   English

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

[英]How to check existence of user-define table type in SQL Server 2008?

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. 我想在使用OBJECT_ID(name, type)函数在补丁中编辑之前检查它是否存在。

What type from the enumeration should be passed for user-defined table types? 什么type枚举应传递用户定义的表类型?

N'U' like for user defined table doesn't work, ie IF OBJECT_ID(N'MyType', N'U') IS NOT NULL N'U'喜欢用户定义的表不起作用,即IF OBJECT_ID(N'MyType', N'U') IS NOT NULL

You can look in sys.types or use TYPE_ID: 您可以查看sys.types或使用TYPE_ID:

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

Just a precaution: using type_id won't verify that the type is a table type--just that a type by that name exists. 只是一个预防措施:使用type_id不会验证类型是否为类型 - 只是存在该名称的类型。 Otherwise gbn's query is probably better. 否则gbn的查询可能会更好。

IF EXISTS (SELECT * FROM sys.types WHERE is_table_type = 1 AND name = 'MyType')
    --stuff

sys.types ... they aren't schema-scoped objects so won't be in sys.objects sys.types ...它们不是模式范围的对象,因此不在sys.objects中

Update, Mar 2013 更新,2013年3月

You can use TYPE_ID too 您也可以使用TYPE_ID

IF EXISTS(SELECT 1 FROM sys.types WHERE name = 'Person' AND is_table_type = 1 AND SCHEMA_ID('VAB') = schema_id)
DROP TYPE VAB.Person;
go
CREATE TYPE VAB.Person AS TABLE
(    PersonID               INT
    ,FirstName              VARCHAR(255)
    ,MiddleName             VARCHAR(255)
    ,LastName               VARCHAR(255)
    ,PreferredName          VARCHAR(255)
);

Following examples work for me, please note "is_user_defined" NOT "is_table_type" 以下示例适合我,请注意“is_user_defined”NOT“is_table_type”

IF TYPE_ID(N'idType') IS NULL
CREATE TYPE [dbo].[idType] FROM Bigint NOT NULL
go

IF not EXISTS (SELECT * FROM sys.types WHERE is_user_defined = 1 AND name = 'idType')
CREATE TYPE [dbo].[idType] FROM Bigint NOT NULL
go

You can use also system table_types view 您还可以使用system table_types视图

IF EXISTS (SELECT *
           FROM   [sys].[table_types]
           WHERE  user_type_id = Type_id(N'[dbo].[UdTableType]'))
  BEGIN
      PRINT 'EXISTS'
  END 

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

相关问题 在哪里可以在SSMS中检查SQL Server 2008中的用户定义表类型?以及如何在其中插入新列? - Where I can check user-define table type in SQL Server 2008 in SSMS? and how can I insert new column into it? 如何从SQL Server 2008中的where子句检查表中数据的存在? - How to check existence of data in a table from a where clause in sql server 2008? 如何在SQL Server 2008中获得临时表的存在 - How to get existence of a temporary table in sql server 2008 在SQL Server表中检查用户是否存在 - Checking for user existence in SQL Server table 如何检查 SQL Server 2008 表中字符串列中的 id - How to check id in a string column in a SQL Server 2008 table 如何在SQL Server 2008中检查表中的状态列? - How to check the status column in a table in SQL Server 2008? 在SQL Server中检查表是否存在的正确方法是什么? - What is the correct way to check for the existence of a table in SQL Server? SQL Server 2008需要使用SQL语句创建用户表类型create语句 - SQL Server 2008 need to create user table type create statement with a SQL statement SQL Server 2008无法使用4个小数位定义小数类型? - SQL Server 2008 cannot define decimal type with 4 decimal places? 通过控制表SQL Server 2008定义选择 - Define a Select via a control table, SQL Server 2008
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM