203

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.

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

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

5 Answers 5

240

You can look in sys.types or use 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. Otherwise gbn's query is probably better.

5
  • 2
    I was naively trying to do IF OBJECT_ID(N'MyType', 'TT') IS NULL with no success, but your solution worked. Apr 18, 2013 at 7:23
  • 1
    The 'TT' enumeration only works in sql server 2012 or later (as i just found out)
    – Iain
    Jul 29, 2015 at 1:34
  • 4
    @Iain It still doesn't, actually. You can't use OBJECT_ID to search for a table type by name -- check out SELECT name FROM sys.objects WHERE type = 'TT'
    – NReilingh
    Nov 29, 2015 at 9:17
  • what is the use of N before 'MyType'? Mar 15, 2022 at 6:30
  • 1
    @NishantKumar The N prefix makes the string literal a Unicode string. Sep 20 at 14:42
139
IF EXISTS (SELECT * FROM sys.types WHERE is_table_type = 1 AND name = 'MyType')
    --stuff

sys.types... they aren't in sys.objects under their normal name

Update, Mar 2013

You can use TYPE_ID too

2
  • 6
    I believe your second comment is inaccurate. If I'm not mistaken, User-Defined Types are indeed schema-scoped (The Schema_ID is in fact one of the attributes in the sys.types table you linked to; this is why they can be referenced as [dbo].[myUDType]). Nevertheless, you are correct that UD types are not listed in sys.objects, and therefore not accessible by OBJECT_ID(). (For whatever reason, sys.objects isn't an exhaustive list of schema-scoped objects.)
    – kmote
    Jun 21, 2012 at 17:09
  • 1
    @kmote - They are not listed in sys.objects directly but there is a row there for each of these Dec 10, 2013 at 0:47
21
IF EXISTS(SELECT 1 FROM sys.types WHERE name = 'Person' AND is_table_type = 1 AND schema_id = SCHEMA_ID('VAB'))
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)
);
2
  • I think this is a more complete answer since it checks schema as well. May 28, 2018 at 10:20
  • Where does the value for schema_id come from? (since it isn't in the OP) Jan 15, 2021 at 19:18
7

Following examples work for me, please note "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
6

You can use also system table_types view

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.