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

在开发过程中,很多时候要把结果集存放到临时表中,常用的方法有两种。

一. SELECT INTO
. 使用select into会自动生成临时表,不需要事先创建 select * into #temp from sysobjects
select * from #temp . 如果当前会话中,已存在同名的临时表 select * into #temp from sysobjects 再次运行,则会报错提示:数据库中已存在名为 '%1!' 的对象。
Msg , Level , State , Line
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 . 利用select into生成一个空表
如果要生成一个空的表结构,不包含任何数据,可以给定一个恒不等式如下: select * into #temp from sysobjects where =
select * from #temp 二. INSERT INTO
. 使用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(),
RoleName nvarchar(),
LoginName nvarchar(),
DefDBName nvarchar(),
DefSchemaName nvarchar(),
UserID smallint,
SID smallint
) insert into #helpuser exec sp_helpuser select * from #helpuser 1.3 保存从动态语句返回的结果集 create table test_dbcc
(
TraceFlag varchar(),
Status tinyint,
Global tinyint,
Session tinyint
) insert into test_dbcc exec('DBCC TRACESTATUS') select * from test_dbcc 对于动态SQL,或者类似DBCC这种非常规的SQL语句,都可以通过这种方式来保存结果集。 . 不能嵌套使用insert exec语句 2.1 下面这个例子,尝试保存sp_help_job的结果集到临时表,发生错误 create table #JobInfo
(
job_id uniqueidentifier,
originating_server nvarchar(),
name nvarchar(),
enabled tinyint,
description nvarchar(),
start_step_id int,
category nvarchar(),
owner nvarchar(),
notify_level_eventlog int,
notify_level_email int,
notify_level_netsend int,
notify_level_page int ,
notify_email_operator nvarchar(),
notify_netsend_operator nvarchar(),
notify_page_operator nvarchar(),
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(),
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 , Level , State , Procedure sp_get_composite_job_info, Line
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中作者提到过
() 首先到打开服务器选项Ad Hoc Distributed Queries exec sp_configure 'show advanced options',
RECONFIGURE
GO
exec sp_configure 'Ad Hoc Distributed Queries',
RECONFIGURE
GO () 通过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

把存储过程结果集SELECT INTO到临时表的更多相关文章

  1. 转:把存储过程结果集SELECT INTO到临时表

    把存储过程结果集SELECT INTO到临时表   在开发过程中,很多时候要把结果集存放到临时表中,常用的方法有两种.   一. SELECT INTO  1. 使用select into会自动生成临 ...

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

    在开发过程中,很多时候要把结果集存放到临时表中,常用的方法有两种. 一. SELECT INTO 1. 使用select into会自动生成临时表,不需要事先创建 select * into #tem ...

  3. SELECT INTO创建临时表

    SELECT INTO创建临时表 SQL Server临时表有两种类型:本地和全局.它们在名称.可见性以及可用性上有区别.本地临时表的名称以单个数字符号 (#) 打头:它们仅对当前的用户连接是可见的: ...

  4. oracle数据库存储过程中的select语句的位置

    导读:在oracle数据库存储过程中如果用了select语句,要么使用"select into 变量"语句要么使用游标,oracle不支持单独的select语句. 先看下这个存储过 ...

  5. 存储过程中使用select……into

    在MySQL存储过程中使用SELECT -INTO语句为变量赋值: 用来将查询返回的一行的各个列值保存到局部变量中. 要求: 查询的结果集中只能有1行. SELECT col_name[,...] I ...

  6. 【转载】Sqlserver存储过程中使用Select和Set给变量赋值

    Sqlserver存储过程是时常使用到的一个数据库对象,在存储过程中会使用到Declare来定义存储过程变量,定义的存储过程变量可以通过Set或者Select等关键字方法来进行赋值操作,使用Set对存 ...

  7. 把存储过程SELECT INTO到临时表

    在开发过程中,很多时候要把结果集存放到临时表中,常用的方法有两种. 一. SELECT INTO1. 使用select into会自动生成临时表,不需要事先创建12 select * into #te ...

  8. T-SQL应用,视图、存储过程、触发器、游标、临时表等

    sqlserver常用操作: 视图.存储过程.触发器.函数 --*********************批处理********************* --[在一个批处理中存有一个语法错误,则所有 ...

  9. mysql 存储过程中使用游标中使用临时表可以替代数组效果

    mysql不支持数组.但有时候需要组合几张表的数据,在存储过程中,经过比较复杂的运算获取结果直接输出给调用方,比如符合条件的几张表的某些字段的组合计算,mysql临时表可以解决这个问题.临时表:只有在 ...

随机推荐

  1. PLC漏洞问题

    1.PLC采用大多是经过裁剪的实时操作系统,比如像linux RT.QNX.VxWorks等,这些实时操作系统广泛应用在通信.军事.航天.等工程领域,但是随之工业与网络的互连爆发出很多问题,常见的PL ...

  2. cf1133 bcdef

    b所有数模k,记录出现次数即可 #include<bits/stdc++.h> using namespace std; int main(){ ]; ]={}; cin>>n ...

  3. python中range()函数的用法

    python中range()函数可创建一个整数列表,一般用在for循环中. range()函数语法: range(start,stop[,step]) 参数说明: star: 计数从star开始.默认 ...

  4. java获取当前时间精确到毫秒

    转载:http://af8991.iteye.com/blog/1217672 import java.text.SimpleDateFormat; import java.util.Date; im ...

  5. C++ Primer 笔记——重载运算

    1.对于二元运算符来说,左侧运算对象传递给第一个参数,而右侧运算对象传递给第二个参数.除了重载的函数调用运算符operator()之外,其他重载元素运算符不能含有默认实参. class test { ...

  6. matlab转c++代码实现(主要包含C++ std::vector,std::pair学习,包含数组与常数相乘,数组相加减,将数组拉成一维向量,图片的读入等内容)

    MATLAB部分: xmap = repmat( linspace( -regionW/2, regionW/2, regionW), regionH, 1 );%linspace [x1,x2,N] ...

  7. Oracle 11g 安装过程中“检查网络配置要求 未执行”解决方法

    正在检查网络配置要求... 检查完成.此次检查的总体结果为: 未执行 网上查了一下,很多朋友都遇到这个问题而无从下手,其实解决起来很容易的. 只需要在 Windows XP 中安装 Microsoft ...

  8. Inflated 3D ConvNet 【I3D】

    Two-Stream Inflated 3D ConvNet (I3D) HMDB-51: 80.9% and UCF-101: 98.0% 在Inception-v1 Kinetics上预训练 Co ...

  9. B - Alyona and towers CodeForces - 739C

    链接: https://vjudge.net/contest/202699#problem/B 题意: 给出一个序列,要支持区间加和操作 求其中最长的区间,该区间内的元素满足(ai<ai+1&l ...

  10. [转]pyCharm最新2018激活码

    https://blog.csdn.net/u014044812/article/details/78727496 因公司的需求,需要做一个爬取最近上映的电影.列车号.航班号.机场.车站等信息,所以需 ...