declare @TableName nvarchar(max)
set @TableName='addresses'
DECLARE @sql NVARCHAR(MAX)
set @sql= 'create table #tempadd ('
SELECT @[email protected] + STUFF(-- Remove first comma
(
SELECT ', ' + column_name+' '+ case when DATA_TYPE='varchar' then DATA_TYPE +'(500)' else DATA_TYPE end FROM -- create comma separated values
(
SELECT column_name,DATA_TYPE FROM information_schema.columns where table_name = @TableName --Your query here
) AS T FOR XML PATH('')
)
,1,1,'')
set @sql [email protected]+') '
print @sql
--SET @sql='SELECT * into #tempadd FROM '[email protected]+ ' WHERE 1=2'
EXEC sp_executesql @sql
select * from #tempadd
這將導致一個錯誤創建一個臨時表:因爲它是內定義錯誤使用動態SQL
Msg 208, Level 16, State 0, Line 25
Invalid object name '#tempadd'.
爲什麼-1?沒有任何意見 – rahularyansharma 2012-02-18 06:25:37
[Execute sp_executeSql for select ... into #table but但不能選擇Temp表數據](http://stackoverflow.com/questions/8040105/execute-sp-executesql-for-select-成表 - 丁斜面選出-TEMP-表-DA) – 2012-02-20 06:28:43