Wednesday, December 6, 2017

SQL SERVER - How create new type (with type exists check)

Sometimes is better make new type. Later you can use it, for example for new column in table, or in stored procedure. Here is created type for nvarchar(25).
use db;
...
if not exists
  ( select 'x' from sys.systypes
    where
    lower( name ) = 'user'
  )
begin
  create type [dbo].[USER] from nvarchar(25);
end;
And using it in create table statement:
create table [users]
( 
  ident [dbo].[USER] not null primary key,
  [name] [dbo].[USER_NAME],
  [active] bit not null default 1
);

No comments:

Post a Comment