Monday, 14 September 2015

What is user defined datatype in sql server and how to create ?

In SQL server ,we can create our own userdatatype and can be used while table creation.

Create Syntax :

CREATE TYPE [TYPE NAME] FROM [DATE TYPE] [NULL | NOT NULL CONSTRAINT]

Example:

CREATE TYPE  UserType FROM NVARCHAR(10) NULL

Now the type named "UserType" is created as NVARCHAR datatype and it is nullable column.

Drop Syntax :

DROP TYPE [TYPE NAME]

DROP TYPE UserType

we can also add the type using System SP.

Create Syntax :

EXEC SP_ADDTYPE [TYPE NAME],[DATA TYPE(LENGTH)],[NULL | NOT NULL CONSTRAINT]

Example:

Exec Sp_AddType 'UserType','Nvarchar(10)','Null'

Its also possible to alter the type using the following syntax.

Alter Syntax : 

EXEC SP_RENAME [EXISTING TYPE NAME],[NEW TYPE NAME],[USERDATETYPE]

Example: 

Exec Sp_Rename 'UserType','UserTypeName','USERDATATYPE'

No comments:

Post a Comment