1. 程式人生 > 實用技巧 >SqlServer中將查詢到的資料存到一個臨時表中的各種方法

SqlServer中將查詢到的資料存到一個臨時表中的各種方法

記錄流水賬--今天同事遞交了辭職申請,後續我就要接手他負責的部分工作(生管系統、材料系統)。

select top(20)* into #AA from orc630 	--查詢Table中的資料,並將資料加入到臨時表 ‘AA’中
select * from #AA where manfac in('190658017','168026001')--通過查詢臨時表中指定的資料
select * from #AA --查詢整個臨時表的資料

-----------------------------------------------------------------------------------------下面的是引用別人的也許以後會用到---------------------------------------------

---------------------------------------------------引用的地址:https://www.cnblogs.com/sunxi/p/4501285.html---------------------------------------------------

SqlServer中把結果集放到到臨時表的方法

 一. SELECT INTO   1.使用select into會自動生成臨時表,不需要事先建立   select * into #temp from sysobjects   01.把儲存過程結果集SELECT INTO到臨時表   select * from #temp   2.如果當前會話中,已存在同名的臨時表   select * into #temp from sysobjects   再次執行,則會報錯提示:資料庫中已存在名為'%1!'的物件。   Msg 2714,Level 16,State 6,Line 2   There is already an object named '#temp' in the database.   在使用select into前,可以先做一下判斷:   if OBJECT_ID('tempdb..#temp')is not null   drop table #temp   select * into #temp from sysobjects   select * from #temp   3.利用select into生成一個空表   如果要生成一個空的表結構,不包含任何資料,可以給定一個恆不等式如下:   select * into #temp from sysobjects where 1=2   select * from #temp   二. INSERT INTO   1.使用insert into,需要先手動建立臨時表   1.1儲存從select語句中返回的結果集   create table test_getdate(c1 datetime)   insert into test_getdate select GETDATE()   select * from test_getdate   1.2儲存從儲存過程返回的結果集   create table #helpuser   UserName nvarchar(128),   RoleName nvarchar(128),   LoginName nvarchar(128),   DefDBName nvarchar(128),   DefSchemaName nvarchar(128),   UserID smallint,   SID smallint   )   insert into #helpuser exec sp_helpuser   select * from #helpuser   1.3儲存從動態語句返回的結果集   create table test_dbcc   TraceFlag varchar(100),   Status tinyint,   Global tinyint,   Session tinyint   )   insert into test_dbcc exec('DBCC TRACESTATUS')   select * from test_dbcc   對於動態SQL,或者類似DBCC這種非常規的SQL語句,都可以通過這種方式來儲存結果集。   2.不能巢狀使用insert exec語句   2.1下面這個例子,嘗試儲存sp_help_job的結果集到臨時表,發生錯誤   create table #JobInfo   job_id uniqueidentifier,   originating_server nvarchar(128),   name nvarchar(128),   enabled tinyint,   description nvarchar(512),   start_step_id int,   category nvarchar(128),   owner nvarchar(128),   notify_level_eventlog int,   notify_level_email int,   notify_level_netsend int,   notify_level_page int,   notify_email_operator nvarchar(128),   notify_netsend_operator nvarchar(128),   notify_page_operator nvarchar(128),   delete_level int,   date_created datetime,   date_modified datetime,   version_number int,   last_run_date int,   last_run_time int,   last_run_outcome int,   next_run_date int,   next_run_time int,   next_run_schedule_id int,   current_execution_status int,   current_execution_step nvarchar(128),   current_retry_attempt int,   has_step int,   has_schedule int,   has_target int,   type int   )   insert into #JobInfo exec msdb..sp_help_job   返回錯誤資訊:INSERT EXEC語句不能巢狀。   Msg 8164,Level 16,State 1,Procedure sp_get_composite_job_info,Line 72   An INSERT EXEC statement cannot be nested.   展開錯誤資訊中的儲存過程:   exec sp_helptext sp_get_composite_job_info   發現裡面還有個INSERT INTO…EXEC的巢狀呼叫,SQL Server在語法上不支援。   INSERT INTO @xp_results   EXECUTE master.dbo.xp_sqlagent_enum_jobs @can_see_all_running_jobs,@job_owner,@job_id   2.2可以用分散式查詢來避免這個問題,這種寫法在INSIDE SQL Server 2005中作者提到過   (1)首先到開啟伺服器選項Ad Hoc Distributed Queries   exec sp_configure 'show advanced options',1   RECONFIGURE   GO   exec sp_configure 'Ad Hoc Distributed Queries',1   RECONFIGURE   GO   (2)通過OPENROWSET連線到本機,執行儲存過程,取得結果集   使用windows認證   select * into #JobInfo_S1   from openrowset('sqloledb','server=(local);trusted_connection=yes','exec msdb.dbo.sp_help_job')   select * from #JobInfo_S1   使用SQL Server認證   SELECT * INTO #JobInfo_S2   FROM OPENROWSET('SQLOLEDB','127.0.0.1';'sa';'sa_password','exec msdb.dbo.sp_help_job')   SELECT * FROM #JobInfo_S2   這樣的寫法,既免去了手動建表的麻煩,也可以避免insert exec無法巢狀的問題。幾乎所有SQL語句都可以使用。   --dbcc不能直接執行   SELECT a.* into #t   FROM OPENROWSET('SQLOLEDB','127.0.0.1';'sa';'sa_password',   'dbcc log(''master'',3)')AS a   --可以變通一下   SELECT a.* into #t   FROM OPENROWSET('SQLOLEDB','127.0.0.1';'sa';'sa_password',   'exec(''DBCC LOG(''''master'''',3)'')')AS a