question on create type in sql server 2008

  • In SQL server 2008, I can create data type and then use it to add/modify the colum based on that type I created.

    For example, i have a schema named testing, and the objects below:

    create table testing.table1

    (

    ID int identity(1,1) not null

    , somename nvarchar(120) null

    , someamount decimal(10,4) null

    );

    create type testing.somename from nvarchar(120);

    create type testing.someamount from decimal(12,4);

    alter table testing.table1

    alter column somename testing.somename not null;

    alter table testing.table1

    alter column someamount testing.someamount null;

    So, after this, the table would be poiting to these types created as shown below.

    CREATE TABLE [test].[customers](

    [customerid] [int] IDENTITY(1000,1) NOT NULL,

    [name] [test].[Name] NOT NULL,

    [createddatetime] [datetime2](7) NOT NULL,

    [creditlimit] [test].[CurrencyValue] NULL

    ON [PRIMARY]

    My question is why do i need to create these types? I mean I really don't understand the use of it

    and I can't see why it would be helpful? Do you? Any input would be appreciated. Were they created

    in SQL Server 2008 to replace rules or something?

  • They can come in pretty handy if you have the same datatype stored in multiple locations that are either enterprise or business specific. For example you might create a datatype for ProductCode as varchar(22). Then any table that need ProductCode can use that datatype instead of having to go lookup how big the field is. They are not new to 2008. They were available at least as far back as 2000. There may be some other uses for them but that is how I have used them in the past.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Okay, I see, thanks Sean.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply