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

2021-09-08 10:57:58 字數 4132 閱讀 9453

一. select into

1. 使用select into會自動生成臨時表,不需要事先建立

select * into #temp from sysobjects

01. 把儲存過程結果集select into到臨時表

select * from #temp

2. 如果當前會話中,已存在同名的臨時表

select * into #temp from sysobjects

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

mysql查詢結果翻轉 如何把sql結果集翻轉

我用的是sql 請教如何把sql結果集翻轉?如下一張表 checkinout 顯示員工簽到,簽退的考勤表,checktype 考勤型別 i 表示簽到,o 表示簽退 timeflag 4表示上午,5表示下午 checktime 簽到,籤 userid checktype checktime timef...

sql server 查詢結果集 列 轉行 過程

該方法,實現了,sql server 查詢出的結果集,先放入臨時表,通過臨時表,把列轉為行。1.把結果放入臨時表 select into tab from dbo.operatedefine 2.定義返回表變數 declare rettable table rowid int,values nvar...

sql server 建立儲存過程,顯示結果集

判斷是否存在所要建立的儲存過程名稱 if exists select name from sysobjects where name storedproc1 and type p 存在所要建立的儲存過程則刪除 drop procedure storedproc1 go 建立儲存過程 create p...