如何将数据插入临时表

时间:2016-11-18 14:45:07

标签: tsql common-table-expression temp-tables

有没有办法将此查询的结果插入SSMS中的临时表?我尝试了很多方法但到目前为止失败了。

或者是否有另一种方式,所有我都在寻找它来查询结果并加入另一个我在查询本身似乎无法做的表。

USE CommDB

;With CTE AS (SELECT s.attendanceNumber, 
       MAX(CASE WHEN s.rnk = 1 THEN s.ExamExaminationCode END) as examCode1,
       MAX(CASE WHEN s.rnk = 2 THEN s.ExamExaminationCode END) as examCode2,
       MAX(CASE WHEN s.rnk = 3 THEN s.ExamExaminationCode END) as examCode3,
       MAX(CASE WHEN s.rnk = 4 THEN s.ExamExaminationCode END) as examCode4,
       MAX(CASE WHEN s.rnk = 5 THEN s.ExamExaminationCode END) as examCode5,
       MAX(CASE WHEN s.rnk = 6 THEN s.ExamExaminationCode END) as examCode6,
       MAX(CASE WHEN s.rnk = 7 THEN s.ExamExaminationCode END) as examCode7,
       MAX(CASE WHEN s.rnk = 8 THEN s.ExamExaminationCode END) as examCode8,
       MAX(CASE WHEN s.rnk = 9 THEN s.ExamExaminationCode END) as examCode9,
       MAX(CASE WHEN s.rnk = 10 THEN s.ExamExaminationCode END) as examCode10,
       MAX(CASE WHEN s.rnk = 11 THEN s.ExamExaminationCode END) as examCode11,
       MAX(CASE WHEN s.rnk = 12 THEN s.ExamExaminationCode END) as examCode12,
       MAX(CASE WHEN s.rnk = 13 THEN s.ExamExaminationCode END) as examCode13,
       MAX(CASE WHEN s.rnk = 14 THEN s.ExamExaminationCode END) as examCode14
FROM (
    SELECT  [AttendanceNumber]
           ,[ExaminationDate]
           ,RadiologyID
           ,[ExamExaminationCode]
           ,ROW_NUMBER() OVER(PARTITION BY [AttendanceNumber]
            ORDER BY [ExamExaminationCode]) as rnk --Ordered ASC so examcodes dont move 

      FROM [CommDB].[dbo].[tblRadiologyData] rd 
      where rd.ExaminationDate >= '01 october 2015'
      and   rd.AttendanceSiteCode IN('CNM','RNM') ) s
GROUP BY s.attendanceNumber)

Select c.examCode1,
         c.examCode2,
         c.examCode3,
         c.examCode4,
         c.examCode5,
         c.examCode6,
         c.examCode7,
         c.examCode8,
         c.examCode9,
         c.examCode10,
         c.examCode11,
         c.examCode12,
         c.examCode13,
         c.examCode14,
         c.AttendanceNumber,
         COUNT(c.AttendanceNumber) as [No of occurances]
--       (Select lu.HRGCode from CommDB.dbo.tblRadiologyNucMedLookup lu
--        Where ISNULL(c.examCode1,'') = ISNULL(lu.Exam01,'') 
--        )
from CTE c




GROUP by c.examCode1,
         c.examCode2,
         C.examCode3,
         C.examCode4,
         C.examCode5,
         C.examCode6,
         C.examCode7,
         C.examCode8,
         C.examCode9,
         C.examCode10,
         C.examCode11,
         C.examCode12,
         C.examCode13,
         C.examCode14,
         C.AttendanceNumber


ORDER BY C.examCode1

1 个答案:

答案 0 :(得分:0)

使用pivot声明,您的生活将变得更加轻松 - 您不需要cte。

您的问题 - 有几种类型的临时表:

  1. 全局临时表 - 以##为前缀(您可以从其他会话访问此表)
  2. 会话临时表 - 以#为前缀(您只能从会话访问此表)
  3. 变量临时 - 必须声明并以@为前缀(您只能在一个批次中访问此表)
  4. 关于tempdb的表 - 公共表,但管理任务(重启服务)将被删除
  5. 其他:

    1. 您可以使用视图或TVF而不是临时表(这是另一种方式)
    2. 你可以利用CTE的潜力(大多数人只将它用作视图)
    3. 类型1,2,4可以创建为标准表(使用create statement或script select into)。 类型3你必须声明为变量。

      会话临时表示例:

          create table #TmpTable (id int, col1 varchar(5), col2 varchar(5))
      
          insert into #TmpTable (id, col1, col2) select 1, 'KSDFA', 'ASDAS' -- from tbl