如何在没有声明表的情况下选择* INTO [tmp表]?

时间:2021-09-26 16:28:55

i want use select statement on the a table and inserting result into a temp table variable, but i don't declare temp table with columns and i want use like this:

我想在一个表上使用select语句并将结果插入到临时表变量中,但是我没有使用列声明临时表,我希望像这样使用:

Declare #tmp table;

SELECT * INTO #tmp FROM myTable

this want declare columns and data types for #tmp

这需要声明#tmp的列和数据类型

please help me

请帮帮我

2 个解决方案

#1


33  

You can do this simply without the DECLARE command - which is not valid for #temp tables anyway, only @table variables. Did you try just the following without trying to define #tmp first:

您可以在没有DECLARE命令的情况下完成此操作 - 无论如何,这对于#temp表无效,只有@table变量。您是否尝试过以下而不尝试首先定义#tmp:

SELECT * INTO #tmp FROM myTable;

#2


29  

With data:

有数据:

select *
into #tmp
from myTable

No data:

没有数据:

select *
into #tmp
from myTable
where 0=1

BTW, you can not do this with table variables.

顺便说一句,你不能用表变量做到这一点。

select *
into @tmp
from myTable

Table variables need to be declared with the columns.

需要使用列声明表变量。

#1


33  

You can do this simply without the DECLARE command - which is not valid for #temp tables anyway, only @table variables. Did you try just the following without trying to define #tmp first:

您可以在没有DECLARE命令的情况下完成此操作 - 无论如何,这对于#temp表无效,只有@table变量。您是否尝试过以下而不尝试首先定义#tmp:

SELECT * INTO #tmp FROM myTable;

#2


29  

With data:

有数据:

select *
into #tmp
from myTable

No data:

没有数据:

select *
into #tmp
from myTable
where 0=1

BTW, you can not do this with table variables.

顺便说一句,你不能用表变量做到这一点。

select *
into @tmp
from myTable

Table variables need to be declared with the columns.

需要使用列声明表变量。