Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent to typedef in Transact-SQL?

Is there any way of aliasing datatypes in T-SQL (SQL Server 2005), a bit like a C typedef? I'd like to be able to alias, say, VARCHAR(20) as 'my_datatype' so that wherever I wish to use VARCHAR(20) I could instead use my_datatype.

Synonyms allow you to do this sort of thing for tables, and there are built-in synonyms for some datatypes too, but AFAICS it is not possible to define your own datatype synonyms. Anyone know any different?

like image 472
Ewen Cartwright Avatar asked Dec 18 '22 03:12

Ewen Cartwright


2 Answers

What about this

CREATE TYPE  [schema_name.]typename
FROM system_data_type_name [(precision,scale)] [NULL|NOT NULL]

For example

CREATE TYPE CountryCode
FROM char(2) NULL
like image 160
Gustaf Carleson Avatar answered Jan 01 '23 08:01

Gustaf Carleson


What you're looking for is User Defined Datatypes, or UDD's.

like image 44
ninesided Avatar answered Jan 01 '23 10:01

ninesided