日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

01. 把存储过程结果集SELECT INTO到临时表

發(fā)布時(shí)間:2025/7/14 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 01. 把存储过程结果集SELECT INTO到临时表 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
原文:01. 把存儲過程結(jié)果集SELECT INTO到臨時(shí)表

在開發(fā)過程中,很多時(shí)候要把結(jié)果集存放到臨時(shí)表中,常用的方法有兩種。

一. SELECT INTO
1. 使用select into會自動(dòng)生成臨時(shí)表,不需要事先創(chuàng)建

select * into #temp from sysobjects select * from #temp

?

2. 如果當(dāng)前會話中,已存在同名的臨時(shí)表

select * into #temp from sysobjects

?

再次運(yùn)行,則會報(bào)錯(cuò)提示:數(shù)據(jù)庫中已存在名為 '%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 #tempselect * into #temp from sysobjects select * from #temp

?

3. 利用select into生成一個(gè)空表
如果要生成一個(gè)空的表結(jié)構(gòu),不包含任何數(shù)據(jù),可以給定一個(gè)恒不等式如下:

select * into #temp from sysobjects where 1=2 select * from #temp

?

?

二. INSERT INTO
1. 使用insert into,需要先手動(dòng)創(chuàng)建臨時(shí)表

1.1 保存從select語句中返回的結(jié)果集

create table test_getdate(c1 datetime)
insert into test_getdate select GETDATE()
select * from test_getdate

?

1.2 保存從存儲過程返回的結(jié)果集

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_helpuserselect * from #helpuser

?

1.3 保存從動(dòng)態(tài)語句返回的結(jié)果集

create table test_dbcc ( TraceFlag varchar(100), Status tinyint, Global tinyint, Session tinyint )insert into test_dbcc exec('DBCC TRACESTATUS')select * from test_dbcc

?

對于動(dòng)態(tài)SQL,或者類似DBCC這種非常規(guī)的SQL語句,都可以通過這種方式來保存結(jié)果集。

?

2. 不能嵌套使用insert exec語句

2.1 下面這個(gè)例子,嘗試保存sp_help_job的結(jié)果集到臨時(shí)表,發(fā)生錯(cuò)誤

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

?

返回錯(cuò)誤信息:INSERT EXEC 語句不能嵌套。
Msg 8164, Level 16, State 1, Procedure sp_get_composite_job_info, Line 72
An INSERT EXEC statement cannot be nested.

展開錯(cuò)誤信息中的存儲過程:

exec sp_helptext sp_get_composite_job_info

?

發(fā)現(xiàn)里面還有個(gè)INSERT INTO…EXEC的嵌套調(diào)用,SQL Server在語法上不支持。

INSERT INTO @xp_results EXECUTE master.dbo.xp_sqlagent_enum_jobs @can_see_all_running_jobs, @job_owner, @job_id

?

?

2.2 可以用分布式查詢來避免這個(gè)問題,這種寫法在INSIDE SQL Server 2005中作者提到過
(1) 首先到打開服務(wù)器選項(xiàng)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連接到本機(jī),運(yùn)行存儲過程,取得結(jié)果集
使用windows認(rèn)證

select * into #JobInfo_S1 from openrowset('sqloledb', 'server=(local);trusted_connection=yes','exec msdb.dbo.sp_help_job')select * from #JobInfo_S1

?

使用SQL Server認(rèn)證

SELECT * INTO #JobInfo_S2 FROM OPENROWSET('SQLOLEDB','127.0.0.1';'sa';'sa_password','exec msdb.dbo.sp_help_job')SELECT * FROM #JobInfo_S2

?

這樣的寫法,既免去了手動(dòng)建表的麻煩,也可以避免insert exec 無法嵌套的問題。幾乎所有SQL語句都可以使用。

--dbcc不能直接運(yùn)行 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

?


總結(jié)

以上是生活随笔為你收集整理的01. 把存储过程结果集SELECT INTO到临时表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。