Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 是否可以在不定义模式的情况下插入表变量?_Sql_Sql Server 2005 - Fatal编程技术网

Sql 是否可以在不定义模式的情况下插入表变量?

Sql 是否可以在不定义模式的情况下插入表变量?,sql,sql-server-2005,Sql,Sql Server 2005,是否可以在不定义模式的情况下插入表变量 我需要这样做 Declare @tab1 as table Insert into @tab1 select * from table2 Select * from @tab1 我正在尝试此查询,但出现错误 请提供帮助。此行出现错误: 将@tab1声明为表 “table”附近的语法不正确 但您的插入也犯了一个错误,应该是: select * into tab1 from table2 据我所知,在使用表变量时需要声明模式,如果使用临时表,则不需要声

是否可以在不定义模式的情况下插入表变量

我需要这样做

Declare @tab1 as table

Insert into @tab1 select * from table2

Select * from @tab1
我正在尝试此查询,但出现错误


请提供帮助。

此行出现错误:

将@tab1声明为表

“table”附近的语法不正确

但您的插入也犯了一个错误,应该是:

select * into tab1
from table2
据我所知,在使用表变量时需要声明模式,如果使用临时表,则不需要声明模式,这样就可以了

select * into #tab1
from table2
请参见

这将起作用:

declare @tab table (BillID int)
insert into @tab
select top 10 BillID from tblBill
select * from @tab
这是行不通的:

select top 10 BillID into @tab from tblBill
select * from @tab
如果要动态定义表var的架构,可以使用以下方法:

declare @str varchar(1000)
set @str = 'declare @tab table (BillID int) ' + 
'insert into @tab ' + 'select top 10 BillID from tblBill ' + 'select * from @tab '
exec(@str)

另外,在Pinal Dave的网站上查找()以获得一些好的建议

错误在第一行

Msg 102,15级,状态1,第1行 “table”附近的语法不正确

请尝试将@tab1声明为表,而不是

将@tab1声明为表(col1 varchar(100))即完整表模式

e、 g

将@tab1声明为表(col1 瓦查尔(100))

插入@tab1选择[人名] 来自tblInformation

从@tab1中选择*

输出:

col1

xyz
abc