SqlServer中将查询到的数据存到一个临时表中的各种方法

SqlServer中将查询到的数据存到一个临时表中的各种方法

2023年7月13日发(作者:)

SqlServer中将查询到的数据存到⼀个临时表中的各种⽅法记录流⽔账--今天同事递交了辞职申请,后续我就要接⼿他负责的部分⼯作(⽣管系统、材料系统)。select top(20)* into #AA from orc630 --查询Table中的数据,并将数据加⼊到临时表 ‘AA’中select * from #AA where manfac in('190658017','168026001')--通过查询临时表中指定的数据select * from #AA --查询整个临时表的数据 -----------------------------------------------------------------------------------------下⾯的是引⽤别⼈的也许以后会⽤到---------------------------------------------SqlServer中把结果集放到到临时表的⽅法 ⼀. 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 _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 _help_job')

  select * from #JobInfo_S1

  使⽤SQL Server认证

  SELECT * INTO #JobInfo_S2

  FROM OPENROWSET('SQLOLEDB','127.0.0.1';'sa';'sa_password','exec _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

  

发布者:admin,转转请注明出处:http://www.yc00.com/news/1689249880a225757.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信