把存储过程结果集SELECT INTO到临时表
在开发过程中,很多时候要把结果集存放到临时表中,常用的方法有两种。
一. SELECT INTO
1. 使用select into会自动生成临时表,不需要事先创建
select * into #temp from sysobjects
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连接到本机,运行存储过程,取得结果集
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到临时表
把存储过程结果集SELECT INTO到临时表 在开发过程中,很多时候要把结果集存放到临时表中,常用的方法有两种. 一. SELECT INTO . 使用select into会自动生成临时表,不需要 ...
- 01. 把存储过程结果集SELECT INTO到临时表
在开发过程中,很多时候要把结果集存放到临时表中,常用的方法有两种. 一. SELECT INTO 1. 使用select into会自动生成临时表,不需要事先创建 select * into #tem ...
- oracle数据库存储过程中的select语句的位置
导读:在oracle数据库存储过程中如果用了select语句,要么使用"select into 变量"语句要么使用游标,oracle不支持单独的select语句. 先看下这个存储过 ...
- 存储过程中使用select……into
在MySQL存储过程中使用SELECT -INTO语句为变量赋值: 用来将查询返回的一行的各个列值保存到局部变量中. 要求: 查询的结果集中只能有1行. SELECT col_name[,...] I ...
- 【转载】Sqlserver存储过程中使用Select和Set给变量赋值
Sqlserver存储过程是时常使用到的一个数据库对象,在存储过程中会使用到Declare来定义存储过程变量,定义的存储过程变量可以通过Set或者Select等关键字方法来进行赋值操作,使用Set对存 ...
- SELECT INTO创建临时表
SELECT INTO创建临时表 SQL Server临时表有两种类型:本地和全局.它们在名称.可见性以及可用性上有区别.本地临时表的名称以单个数字符号 (#) 打头:它们仅对当前的用户连接是可见的: ...
- MySQL存储过程中使用SELECT …INTO语句为变量赋值
使用SELECT …INTO语句为变量赋值 在MySQL存储过程中,可以使用SELECT …INTO语句对变量进行赋值,该语句在数据库中进行查询,并将得到的结果赋值给变量.SELECT …INTO语句 ...
- 把存储过程SELECT INTO到临时表
在开发过程中,很多时候要把结果集存放到临时表中,常用的方法有两种. 一. SELECT INTO1. 使用select into会自动生成临时表,不需要事先创建12 select * into #te ...
- oracle存储过程+游标处理select数据
create or replace PROCEDURE UPDATE_RECORDCODE is cursor location_data is select * from location wher ...
随机推荐
- tomcat 调优-生产环境必备
目录 1. tomcat 启动慢 1.1 tomcat 获取随机值阻塞 1.2 tomcat 需要部署的web应用程序太多 1.3 tomcat启动内存不足 2 Connector 调优 2.2 Co ...
- 使用Hexo & Github,搭建属于自己的博客
经过在网上各种找资料,踩过各种坑,终于搭建好了hexo,直接与gitbub一起使用,搭建起自己的免费的博客. 关于Hexo Hexo是一款基于Node.js的静态博客框架. Hexo特性 风一般的速度 ...
- [开源项目]Shell4Win,一个在Windows下执行shell命令的解释器
背景 顺利拿到心目中的理想offer之后,心里的负担一下减轻了很多,希望利用还没毕业之前这段难得的悠闲时间做一点有意义的事情.于是希望能做一个长久以来都想做的开源项目,就是题中提到的Windows下的 ...
- python学习: 优秀Python学习资源收集汇总--转
Python是一种面向对象.直译式计算机程序设计语言.它的语法简捷和清晰,尽量使用无异义的英语单词,与其它大多数程序设计语言使用大括号不一样,它使用縮进来定义语句块.与Scheme.Ruby.Perl ...
- 获取当前iframe动态加载文档的href
Insus.NET想实现一个功能,一个旧的站点A,它有两个网页logon.aspx和Default.aspx(登录成功能访问).由于某些原因,需另建一个新站点B,这个新站点B也有两个网页B_Index ...
- DataTable数据显示于MVC应用程序
这篇博文是把DataTable的数据显示于MVC的应用程序上. 首先我们在数据库中创建一个表,并添加数据,然后创建存储过程: 接下来,我们去下载一个BusinessBase组件:http://www. ...
- .Net下SQLite的DBHelp
怎样获取SqLite请参考初识SqlLite ---.net连接数据库,怎样在SQLite使用Linq请参考在C#中利用Nuget包使用SQLite数据库和Linq to SQLite using S ...
- C# 数组 二维数组
数组:相同数据类型的元素按一定顺序排列的集合.是一组变量 作用:操作大量数据 数组的定义1.数组里面的内容必须是同一类型2.数据必须有长度限制 ...
- 应用rbac组件 动态生成一级菜单
动态生成一级菜单 改表结构 需要知道是否是菜单\icon\名称权限表 +字段: is_menu = models.BooleanField(max_length=32,verbose_name='是否 ...
- vue 子组件和父组件
作者QQ:1095737364 QQ群:123300273 欢迎加入! 1.添加子组件 1.父组件修改 <template> <!-- v-for 表情表示循环输出数据 ...