Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropping / recreating type in SQL Server

IF OBJECT_ID('[dbo].[Order]') IS NOT NULL
    DROP TYPE [dbo].[Order]
GO

CREATE TYPE [dbo].[Order] AS TABLE 
(
    [Id] INT NULL,
    [Order] INT NULL
);

This code doesn't drop the table. I get this error:

The type 'dbo.Order' already exists, or you do not have permission to create it.

How I can drop/recreate type in SQL Server?

like image 730
John Avatar asked Jun 12 '17 11:06

John


People also ask

How do you drop a type in SQL?

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. When a table is dropped, all associated triggers are automatically dropped.

How do I drop a SP in SQL?

Using SQL Server Management Studio Expand Databases, expand the database in which the procedure belongs, and then expand Programmability. Expand Stored Procedures, right-click the procedure to remove, and then click Delete. To view objects that depend on the procedure, click Show Dependencies.


1 Answers

Any of your stored procedure may be using this table type. That's why it doesn't allow you to drop. You may be getting following error.

Msg 3732, Level 16, State 1, Line 4 Cannot drop type 'tabletype' because it is being referenced by object 'storedprocedurename'. There may be other objects that reference this type.

Msg 219, Level 16, State 1, Line 3 The type 'tabletype' already exists, or you do not have permission to create it.

check that storedprocedure or function name. just comment the usage for a while and once reference is removed. you can drop and recreate the table type. after that uncomment the reference usage in stored procedure / function.

This will surely works !!

like image 189
Ketan Kotak Avatar answered Oct 12 '22 06:10

Ketan Kotak