x

Tabletype as framework for new tables

I am playing with tabletypes and it seems you can only use this for passing tabledata to a stored procedure. It would be nice if a tabletype can be used as a template for tables like:

CREATE TYPE [EnumList] 
AS TABLE (
         ID int identity(1,1) PRIMARY KEY,
         Value varchar(32) NOT NULL UNIQUE,
         IsActive bit NOT NULL DEFAULT(1),
         CreatedBy sysname NOT NULL default SUSER_NAME(),
         CreatedDate smalldatetime NOT NULL default CURRENT_TIMESTAMP
       )
GO

create table enum_valuta as EnumList -- not working
GO
However this gives an error. Am I missing something? If this would work, SQL will become a little more object-oriented :)
more ▼

asked May 03, 2012 at 11:20 AM in Default

Wilfred van Dijk gravatar image

Wilfred van Dijk
1.2k 18 22 30

Yeah, but it isn't.
May 03, 2012 at 11:54 AM Grant Fritchey ♦♦
(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

You can use the type to declare a table variable though. And then use that to make a table using SELECT INTO...

CREATE TYPE [EnumList] 
AS TABLE (
         ID int identity(1,1) PRIMARY KEY,
         Value varchar(32) NOT NULL UNIQUE,
         IsActive bit NOT NULL DEFAULT(1),
         CreatedBy sysname NOT NULL default SUSER_NAME(),
         CreatedDate smalldatetime NOT NULL default CURRENT_TIMESTAMP
       )
GO

declare @enum_valuta as EnumList

insert into @enum_valuta (Value, IsActive) select 'aaaa',1
insert into @enum_valuta (Value, IsActive) select 'bbbb',1
insert into @enum_valuta (Value, IsActive) select 'cccc',0

select * into Enum_valuta from @enum_valuta

select * from Enum_valuta

drop type [EnumList]
drop table Enum_valuta
more ▼

answered May 03, 2012 at 11:27 AM

Kev Riley gravatar image

Kev Riley ♦♦
53k 47 49 76

Yes, I know, but this is for in memory operations. What confuses me is that this statement is working:

create type SSN_type from varchar(11) go create table person(name varchar(32), SSN SSN_type) go

While my previous example is not working
May 03, 2012 at 11:50 AM Wilfred van Dijk
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x986
x7

asked: May 03, 2012 at 11:20 AM

Seen: 696 times

Last Updated: May 03, 2012 at 11:54 AM