把存储过程SELECT INTO到临时表
在开发过程中,很多时候要把结果集存放到临时表中,常用的方法有两种。
一. SELECT INTO
1. 使用select into会自动生成临时表,不需要事先创建
12 select * into #temp from sysobjects
select * from #temp
2. 如果当前会话中,已存在同名的临时表
1 select * into #temp from sysobjects
再次运行,则会报错提示:数据库中已存在名为'%1!' 的对象。www.it165.net
Msg 2714, Level16, State 6, Line 2
There is alreadyan object named '#temp' in the database.
在使用select into前,可以先做一下判断:
1234 if OBJECT_ID('tempdb..#temp') is not null
drop table #temp
select * into #temp from sysobjects
select * from #temp
3. 利用select into生成一个空表
如果要生成一个空的表结构,不包含任何数据,可以给定一个恒不等式如下:
12 select * into #temp from sysobjects where 1=2
select * from #temp
二. INSERT INTO
1. 使用insert into,需要先手动创建临时表
1.1 保存从select语句中返回的结果集
123 create table test_getdate(c1 datetime)
insert into test_getdate select GETDATE()
select * from test_getdate
1.2 保存从存储过程返回的结果集
123456789101112 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 保存从动态语句返回的结果集
123456789 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语句
下面这个例子,尝试保存sp_help_job的结果集到临时表,发生错误:
123456789101112131415161718192021222324252627282930313233343536 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, Level16, State 1, Procedure sp_get_composite_job_info, Line 72
An INSERT EXEC statement cannot benested.
展开错误信息中的存储过程:
1 exec sp_helptext sp_get_composite_job_info
发现里面还有个INSERT INTO…EXEC的嵌套调用,SQL Server在语法上不支持。
12 INSERT INTO @xp_results
EXECUTE master.dbo.xp_sqlagent_enum_jobs @can_see_all_running_jobs, @job_owner, @job_id
可以用分布式查询来避免这个问题,这种写法在INSIDE SQL Server 2005中作者提到过。
(1) 首先到打开服务器选项Ad Hoc Distributed Queries
123456 exec sp_configure 'show advanced options',1
RECONFIGURE
GO
exec sp_configure 'Ad Hoc Distributed Queries',1
RECONFIGURE
GO
(2) 通过OPENROWSET连接到本机,运行存储过程,取得结果集
使用windows认证
123 select * into #JobInfo_S1
from openrowset('sqloledb', 'server=(local);trusted_connection=yes','exec msdb.dbo.sp_help_job')
select * from #JobInfo_S1
使用SQLServer认证
123 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语句都可以使用。
12345678 --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
把存储过程SELECT INTO到临时表的更多相关文章
- 把存储过程结果集SELECT INTO到临时表
把存储过程结果集SELECT INTO到临时表 在开发过程中,很多时候要把结果集存放到临时表中,常用的方法有两种. 一. SELECT INTO . 使用select into会自动生成临时表,不需要 ...
- 转:把存储过程结果集SELECT INTO到临时表
把存储过程结果集SELECT INTO到临时表 在开发过程中,很多时候要把结果集存放到临时表中,常用的方法有两种. 一. SELECT INTO 1. 使用select into会自动生成临 ...
- SQL Server数据库的存储过程中定义的临时表,真的有必要显式删除临时表(drop table #tableName)吗?
本文出处:http://www.cnblogs.com/wy123/p/6704619.html 问题背景 在写SQL Server存储过程中,如果存储过程中定义了临时表,有些人习惯在存储过程结束的时 ...
- SELECT INTO创建临时表
SELECT INTO创建临时表 SQL Server临时表有两种类型:本地和全局.它们在名称.可见性以及可用性上有区别.本地临时表的名称以单个数字符号 (#) 打头:它们仅对当前的用户连接是可见的: ...
- Java调用oracle存储过程通过游标返回临时表数据
注:本文来源于 < Java调用oracle存储过程通过游标返回临时表数据 > Java调用oracle存储过程通过游标返回临时表数据 项目开发过程中,不可避免的会用到存储过程返回结 ...
- 01. 把存储过程结果集SELECT INTO到临时表
在开发过程中,很多时候要把结果集存放到临时表中,常用的方法有两种. 一. SELECT INTO 1. 使用select into会自动生成临时表,不需要事先创建 select * into #tem ...
- 存储过程使用表变量或临时表代替游标Fetch实例,访问远程数据库
定义表变量是可以直接操作在内存中的数据,比较快.临时表在大数据量时会比游标使用的资源少.还是要看具体情况了.也有可能在实际优化过程中相互替换呢. 留作记忆的代码如下: if object_id('te ...
- MYSQL 存储过程、函数、临时表、游标
创建函数 因为我们平时经常需要创建不同日期的数据,以模拟的场景,覆盖更多的用例,所以这里写了一个返回随机日期的demo.大家可以自行扩展. DROP FUNCTION IF EXISTS milan_ ...
- 解决 FastReport 使用存储过程 找不到临时表问题
在存储过程最开始加入:以下命令就可以了 SET FMTONLY OFF 有时候在执行SQL查询语句时,仅仅需要知道检索的元数据,而不是具体的数据行,可以设置SET FMTONLY ON. SET FM ...
随机推荐
- SDL检查
在用 Visual Studio 编译比较早的代码时,经常会遇到错误: 错误 C4996 'wcscpy': This function or variable may be unsafe. Cons ...
- java基本类型的大小
1个字节是8位byte 1字节short 2字节int 4字节long 8字节float 4字节double 8字节char 2字节boolean 1字节======================= ...
- windowsphone开发页面跳转到另一个dll中的页面
WP的页面跳转一般是只能跳转到本DLL的页面, 如果要跳转到其他DLL的页面则需要这样写 (Application.Current.RootVisual as PhoneApplicationFram ...
- iOS多线程编程:线程同步总结
1:原子操作 - OSAtomic系列函数 iOS平台下的原子操作函数都以OSAtomic开头,使用时需要包含头文件<libkern/OSBase.h>.不同线程如果通过原子操作函数对同一 ...
- STL标准库-一个万用的hash function
技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 在前面我介绍过hash的使用,本次主要介绍一下Hash Function Hash Function即获得hash code的函 ...
- Mysql数据库的读写分离
读写分离,即在主数据库中进行写操作(也可以进行增.删.改操作),在从数据库中进行读操作.在正常情况下,我们对主数据库进行的是增.删.改.查操作,数据库的写入时间比较长,而查询时间短,所以为了提高数据库 ...
- jQuery实现按Esc清除信息功能
1.jQuery实现按Esc清除信息功能 function clearLogs(){ $(".logs").empty(); } $(document).ready(fu ...
- 多重条件判断SQL:用于用户名称,密码,权限的检测和判断
string sqlstr = "select count(*) from tb_admin where 用户名='"+UserName+"'and 密码='" ...
- nw 系统托盘的添加方式,以及ajax失效问题
1.nw 系统托盘的添加方式 /** ------------------------------------------------------------ 最小化托盘 -------------- ...
- ubuntu16安装mysql图形界面
之前在windows用sqlyog当做图形界面连接mysql,现在在ubuntu上需要连接测试环境的数据库,需要安装mysql图形界面.安装只需要条命令: sudo apt-get update su ...