是否可以在不定義模式的情況下插入表變量?是否可以在不定義模式的情況下插入表變量?
我需要做的這個像
Declare @tab1 as table
Insert into @tab1 select * from table2
Select * from @tab1
我嘗試此查詢,但得到的錯誤。
請幫忙。
是否可以在不定義模式的情況下插入表變量?是否可以在不定義模式的情況下插入表變量?
我需要做的這個像
Declare @tab1 as table
Insert into @tab1 select * from table2
Select * from @tab1
我嘗試此查詢,但得到的錯誤。
請幫忙。
錯誤在第一行
Msg 102,Level 15,State 1,Line 1 'table'附近的語法不正確。
代替申報@ TAB1如表嘗試
申報@ TAB1如表(COL1 VARCHAR(100)),即全表模式
例如
申報@ TAB1如表(COL1 VARCHAR(100))
插入@ TAB1從@從tblInformation
選擇*選擇[用戶名] TAB1
輸出:
個COL1
XYZ
ABC
錯誤與該行:
申報@ TAB1如表
附近有語法錯誤 '表'。
,但你也取得了與插入一個錯誤應該是:
select * into tab1
from table2
據我知道你需要使用表可變因素時,申報方案,如果使用臨時表你不」牛逼所以這會工作
select * into #tab1
from table2
見msdn
這將工作:
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的網站上查找(sp_executesql)以獲得一些好的建議
我喜歡你的個人檔案相片:) – leppie 2009-10-09 10:06:06