I'm currently working on a script in T-SQL in SQL Server 2014.
I need to drop a user-defined table type, but only if it exists, and create it again after the delete/drop type.
I did some research on the web and found a solution, which does, unfortunately, not work at all.
My current script looks like this:
IF OBJECT_ID('MySchema.tProjectType', 'U') IS NOT NULL
DROP TYPE [MySchema].[tProjectType];
CREATE TYPE [MySchema].[tProjectType] AS TABLE
(
Id INT
, IsPrivate BIT
, IsPublic BIT
);
My error message:
The type 'MySchema.tProjectType' already exists, or you do not have permission to create it.
Do you know how to successfully check if a user defined table type exists before I can delete it in SQL Server 2014?
Remove an alias data type or user-defined type (CLR) from the current database. Syntax DROP TYPE [schema.] type [ ; ] Key type Name of the type (alias or user-defined) to be dropped.
You can drop a user-defined type (UDT) using the DROP statement. You cannot drop a UDT if it is used: In a column definition for an existing table or view.
The DROP TABLE statement deletes the specified table, and any data associated with it, from the database. The IF EXISTS clause allows the statement to succeed even if the specified tables does not exist. If the table does not exist and you do not include the IF EXISTS clause, the statement will return an error.
Please try this, use type_id instead of object_id
IF type_id('[MySchema].[tProjectType]') IS NOT NULL
DROP TYPE [MySchema].[tProjectType];
CREATE TYPE [MySchema].[tProjectType] AS TABLE
(
Id INT
, IsPrivate BIT
, IsPublic BIT
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With