如何从类型创建临时表? [英] How do I create a temporary table from a 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
另外,从这里:根据用户定义的类型创建表:
CREATE TABLE #superBrand OF usr.NameList -- doesn't work
推荐答案
Pure.Krome 的回答 展示了如何使用表变量而不是#temp 表.如果你真的想要一个基于已知表类型的 #temp 表(不必知道列名/定义),你可以说:
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;
现在,#superBrand
应该匹配表类型的表结构,减去约束(以及从 SQL Server 2014 开始的不太有用的二级索引).
Now, #superBrand
should match the table structure of the table type, minus the constraints (and marginally useful secondary indexes, starting with SQL Server 2014).
当然,随后填充#temp 表的其余代码必须知道结构.那么,声明一个与表类型结构相同的#temp表的目的究竟是什么?
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?
这篇关于如何从类型创建临时表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!