Alter user defined type in SQL Server
This is what I normally use, albeit a bit manual:
/* Add a 'temporary' UDDT with the new definition */
exec sp_addtype t_myudt_tmp, 'numeric(18,5)', NULL
/* Build a command to alter all the existing columns - cut and
** paste the output, then run it */
select 'alter table dbo.' + TABLE_NAME +
' alter column ' + COLUMN_NAME + ' t_myudt_tmp'
from INFORMATION_SCHEMA.COLUMNS
where DOMAIN_NAME = 't_myudt'
/* Remove the old UDDT */
exec sp_droptype t_mydut
/* Rename the 'temporary' UDDT to the correct name */
exec sp_rename 't_myudt_tmp', 't_myudt', 'USERDATATYPE'
Unable to Modify User-Defined Table Type
You have binding in SP_DoSomething
stored procedure. The type you want to change is used in that stored procedure.
You need to save script of that procedure. Drop it. Change dbo.UserDefinedTableType
and create procedure again.
There is a similar post here. Check is some of the answers can help you. Answer of @norlando seems promising.
Copying user-defined types from one database to another
Right click on the database in SQL Server Management Studio, choose Tasks > Generate Scripts...
. Then check user defined data types and table types. Click Next
etc up until Finish
.
Once you have the script generated, execute it on the database you want to create the types in.
How To add composite unique key to user defined table Type
You Can't Alter UserDefined table types ,You need to drop and recreate again for any changes..
From MSDN..
User-defined types cannot be modified after they are created, because changes could invalidate data in the tables or indexes. To modify a type, you must either drop the type and then re-create it, or issue an ALTER ASSEMBLY statement by using the WITH UNCHECKED DATA clause.
Below is the way to create unique constraint on UserDefined Table Type
CREATE TYPE test AS TABLE
( col1 VARCHAR(50)
, col2 INT ,
unique (col1,col2)
);
Note:We can't name Constraints,so creating constraints like normal way is not valid..
Example below
CREATE TYPE test AS TABLE
( col1 VARCHAR(50)
, col2 INT ,
constraint test unique (col1,col2)
);
SQL Server 2012 - Cannot drop User defined (table) type while there are no active dependencies
Renaming is a dangerous pastime because it doesn't mesh well with SQL Server's limited implementation of dependency tracking, and the deferred name resolution of stored procedures. I couldn't actually reproduce this specific scenario on SQL Server 2017 (dependencies were correctly tracked if the UDT is used in the procedure's body) so it's possible this case has been improved. Certainly, if you use the UDT as a parameter it actually detects the problem and gives an appropriate error message:
CREATE TYPE BadUdt FROM NVARCHAR(10);
GO
CREATE PROCEDURE AreUdtsBadThough(@T BadUdt) AS BEGIN
RETURN;
END;
GO
EXEC sp_rename 'BadUdt', 'GoodUdt';
GO
CREATE TYPE BadUdt FROM NVARCHAR(20);
GO
EXEC AreUdtsBadThough NULL
Msg 496, Level 16, State 1, Procedure AreUdtsBadThough, Line 6 [Batch
Start Line 16] The parameter "@T" is not the same type as the type it
was created with. Drop and recreate the module using a two-part name
for the type, or use sp_refreshsqlmodule to refresh its parameters
metadata.
Despite the name change, the parameter of AreUdtsBadThough
is still linked to the old type by ID, which is why you can't drop that:
SELECT p.[name], t.[name]
FROM sys.parameters p
JOIN sys.types t ON p.user_type_id = t.user_type_id
WHERE [object_id] = OBJECT_ID('AreUdtsBadThough')
+------+---------+
| name | name |
+------+---------+
| @T | GoodUdt |
+------+---------+
The error helpfully describes the general solution for dependency problems: invoke sp_refreshsqlmodule
, which forces the stored procedure (or function, or trigger, or view) to effectively be recompiled and the dependencies to be updated:
EXEC sp_refreshsqlmodule 'AreUdtsBadThough'
SELECT p.[name], t.[name]
FROM sys.parameters p
JOIN sys.types t ON p.user_type_id = t.user_type_id
WHERE [object_id] = OBJECT_ID('AreUdtsBadThough')
+------+--------+
| name | name |
+------+--------+
| @T | BadUdt |
+------+--------+
And now GoodUdt
has no dependencies anymore and can be dropped.
The problem of dependencies not always getting updated correctly (which is not restricted to renaming) has prompted some people to come up with more permanent solutions. Aaron Bertrand has an article on automating the dependency refreshes for SQL Server 2008 (which should still work for later editions, even if it may do more work than necessary) due to improved dependency checks.
What permissions to I need to use User Defined Table Types
After doing a bit more digging the permission I needed to add was not execute
it was control
.
So instead of doing this;
grant execute on type::dbo.AFFECTEDSERVICELIST to subaccount
I actually needed to do this;
grant control on type::dbo.AFFECTEDSERVICELIST to subaccount
Related Topics
Is There Auto Increment in SQLite
Why Does Using an Underscore Character in a Like Filter Give Me All the Results
SQL Primary Key: Integer VS Varchar
Finding the Next Available Id in MySQL
SQL Server 2008 Paging Methods
Sqlite Select with Condition on Date
How Exactly Does Using or in a MySQL Statement Differ With/Without Parentheses
Custom Function with Check Constraint SQL Server 2008
SQL Speed Up Performance of Insert
Extracting Hours from a Datetime (SQL Server 2005)
Get Month Name from Date in Oracle
Name Database Design Notation You Prefer and Why
Column Name or Number of Supplied Values Does Not Match Table Definition
Postgres Not Using Index When Index Scan Is Much Better Option
SQL Query That Gives Distinct Results That Match Multiple Columns