Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create SQL Server table based on a user defined type

Tags:

I have my defined table type created with

CREATE TYPE dbo.MyTableType AS TABLE (     Name      varchar(10) NOT NULL,     ValueDate date        NOT NULL,     TenorSize smallint    NOT NULL,     TenorUnit char(1)     NOT NULL,     Rate      float       NOT NULL     PRIMARY KEY (Name, ValueDate, TenorSize, TenorUnit) ); 

and I would like to create a table of this type. From this answer the suggestion was to try

CREATE TABLE dbo.MyNewTable AS dbo.MyTableType 

which produced the following error message in my SQL Server Express 2012:

Incorrect syntax near the keyword 'OF'.

Is this not supported by SQL Server Express? If so, could I create it some other way, for example using DECLARE?

like image 942
gt6989b Avatar asked Mar 13 '14 20:03

gt6989b


People also ask

Can we use user-defined table type inside another user-defined table type in SQL?

A user-defined table type cannot be used as a column in a table or a field in a structured user-defined type.

Can we create user-defined data type in SQL?

To create a user-defined data type. In Object Explorer, expand Databases, expand a database, expand Programmability, expand Types, right-click User-Defined Data Types, and then click New User-Defined Data Type.


1 Answers

--Create table variable from type. DECLARE @Table AS dbo.MyTableType  --Create new permanent/physical table by selecting into from the temp table. SELECT * INTO dbo.NewTable FROM @Table WHERE 1 = 2  --Verify table exists and review structure. SELECT * FROM dbo.NewTable 
like image 118
Dave Mason Avatar answered Sep 20 '22 10:09

Dave Mason