Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
666 views
in Technique[技术] by (71.8m points)

sql - How do I create a temporary table from a type?

Suppose I have some type:

CREATE TYPE usr.NameList AS TABLE ([name] VARCHAR(MAX) NOT NULL);

And now I want to create a temporary table based on this type, how do I do it?

CREATE TABLE #superBrand (usr.NameList) -- doesn't work

Also, from this: create table based on a user defined type:

CREATE TABLE #superBrand OF usr.NameList -- doesn't work
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Pure.Krome's answer shows how to use a table variable instead of a #temp table. If you really want a #temp table based on a known table type (without having to know the column names/definitions), you can say:

DECLARE @d usr.NameList;
SELECT * INTO #superBrand FROM @d;

Now, #superBrand should match the table structure of the table type, minus the constraints (and marginally useful secondary indexes, starting with SQL Server 2014).

Of course, the rest of your code that then populates the #temp table is going to have to know the structure. So, what exactly is the purpose of declaring a #temp table with the same structure as a table type?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...