一. SELECT INTO
 
  1. 使用select into会自动生成临时表,不需要事先创建
 
  select * into #temp from sysobjects
 
  01. 把存储过程结果集SELECT INTO到临时表
 
  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连接到本机,运行存储过程,取得结果集
 
  使用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

SqlServer中把结果集放到到临时表的方法的更多相关文章

  1. SqlServer中把结果集放到到临时表的方法(转)

    一. SELECT INTO   1. 使用select into会自动生成临时表,不需要事先创建   select * into #temp from sysobjects   01. 把存储过程结 ...

  2. Ms SQLServer中的Union和Union All的使用方法和区别

    Ms SQLServer中的Union和Union All的使用方法和区别 SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 ...

  3. sqlserver中set IDENTITY_INSERT on 和 off 的设置方法

    sqlserver中set IDENTITY_INSERT on 和 off 的设置方法: 执行插入数据库插入数据时报了以下错误,我明明没有给主键set值但还是报错 解决方法如下: qlserver ...

  4. 知方可补不足~sqlserver中触发器的使用

    回到目录 触发器在过去的10年中,即存储过程和ado.net称霸江湖期间是那么的重要,而现在,trigger显得不是那么必要的,我们很少将复杂的业务写在SQL里,当然也会没有机会写到trigger里了 ...

  5. SQLSERVER中的假脱机spool

    SQLSERVER中的假脱机spool 我发现网上对于假脱机的解释都非常零散,究竟假脱机是什么? 这几天在家里研究了一下,收集了很多网上的资料 假脱机是中文的翻译,而英文的名字叫做 spool 在徐老 ...

  6. (转)笔记320 SQLSERVER中的加密函数 2013-7-11

    1 --SQLSERVER中的加密函数 2013-7-11 2 ENCRYPTBYASYMKEY() --非对称密钥 3 ENCRYPTBYCERT() --证书加密 4 ENCRYPTBYKEY() ...

  7. Sqlserver中存储过程,触发器,自定义函数(一)

    Sqlserver中存储过程,触发器,自定义函数 1.存储过程有关内容存储过程的定义:存储过程的分类:存储过程的创建,修改,执行:存储过程中参数的传递,返回与接收:存储过程的返回值:存储过程使用游标. ...

  8. Sqlserver中存储过程,触发器,自定义函数

    Sqlserver中存储过程,触发器,自定义函数: 1. 触发器:是针对数据库表或数据库的特殊存储过程,在某些行为发生的时候就会被激活 触发器的分类: DML触发器:发生在数据操作语言执行时触发执行的 ...

  9. 我是如何在SQLServer中处理每天四亿三千万记录的

    首先声明,我只是个程序员,不是专业的DBA,以下这篇文章是从一个问题的解决过程去写的,而不是一开始就给大家一个正确的结果,如果文中有不对的地方,请各位数据库大牛给予指正,以便我能够更好的处理此次业务. ...

随机推荐

  1. Java _ JDK _ Arrays, LinkedList, ArrayList, Vector 及Stack

    (最近在看JDK源码,只是拿着它的继承图在看,但很多东西不记录仍然印象不深,所以开始记录JDK阅读系列.) (一)Arrays Arrays比较特殊,直接继承自Arrays ->List(Int ...

  2. PS(photoshop)里A4纸张的像素是多大?

    A4纸的尺寸是210mm×297mm, 当你设定的分辨率是72像素/英寸时,A4纸的尺寸的图像的像素是595×842, 当你设定的分辨率是150像素/英寸时,A4纸的尺寸的图像的像素是1240×175 ...

  3. [HIHO119]网络流五·最大权闭合子图(最大流)

    题目链接:http://hihocoder.com/contest/hiho119/problem/1 题意:中文题意. 由于1≤N≤200,1≤M≤200.最极端情况就是中间所有边都是满的,一共有N ...

  4. ironpython 2.75 在c#中的使用

    ironpython的介绍请自行搜索. 一句话,python是一个类似lua js的动态预言.ironpython是在net环境执行python的类库. 效果:在网站中调用一个python文件test ...

  5. v9 推荐位 排序问题解决办法

    原网站:http://bbs.phpcms.cn/thread-879943-1-1.html 简介: 用phpcms做网站的时候,有些地方要用到推荐位列表,如幻灯片,特别推荐等.有时候因为文章的重要 ...

  6. js 定时器的使用。 setInterval()

    我需要实现的功能是:点击发送按钮,会出现 “已发送60s后可点击重发”,并且,60s 这个数字是随时变化的,60,59,58,57....0,然后再次返回到 发送 按钮. 类似效果,可参考  360首 ...

  7. centos中更换jdk的版本

    现在讲的是Linux中更换jdk版本的问题,卸载Linux自带的jdk更换sun的jdk百度一大堆,但是如果我安装的sun的jdk是1.7的想更换到1.8的如何解决呢,方法其实超easy. 把1.8的 ...

  8. Spring security3

    最近一直在学习spring security3,试着搭建了环境: 构建maven环境 项目配置pom.xml文件 <project xmlns="http://maven.apache ...

  9. ABAP 没有地方输入\H 进入DEBUG 怎么办?

    把如下代码保存,命名debug.txt  ,把这个文件拖拉到要调试的窗口. [FUNCTION]Command=/HTitle=Barry TestType=SystemCommand

  10. SpringAop学习

    Spring Aop (jdk动态代理和cglib代理) Aop 的概念 aop即面向切面编程,一般解决具有横切面性质的体统(事务,缓存,安全) JDK动态代理: 可以使用实现proxy 类,实现jd ...