【问题标题】:Issue inserting values with a subquery into temporary table SQL Server 2008将带有子查询的值插入临时表 SQL Server 2008 的问题
【发布时间】:2016-03-20 05:50:10
【问题描述】:

尝试使用 SQL Server 2008 将值插入临时表,得到:

消息 116,级别 16,状态 1,过程 Test_temp_table,第 274 行
当子查询不使用 EXISTS 引入时,选择列表中只能指定一个表达式。

使用此查询:

VALUES(
(select 
     a.*, b.[formal name], c.*, d.*
 from 
     selecthr20.employee.[career history] a, 
     selecthr20.Employee.[Current Appointments As At Evaluation Date] b,  
     Employee.[BSK Changes in Selected Period] c,
     selecthr20.employee.[career history extra detail] d
 where 
     a.[appointment number] = b.[appointment number]
     and a.[career number] = c.[primary key number]
     and a.[career number] = d.[career number]
     and c.[primary key number] = d.[career number]
     and c.[primary key name] = 'Career Number'
     and b.[person number] in (select b.[person number] 
                               from employee.[current pay as at evaluation date] 
                               where substring([Payroll Name],1,6) = 'DOV020')))

【问题讨论】:

  • 您似乎使用了错误的语法。 INSERT INTO ... SELECT 根本不使用 VALUES () 表达式。只是INSERT INTO #TempTable SELECT ....
  • Bad habits to kick : using old-style JOINs - 旧式 逗号分隔的表格列表 样式已替换为 ANSI 中的 proper ANSI JOIN 语法-92 SQL 标准(20 多年前),不鼓励使用它

标签: sql sql-server sql-server-2008 tsql


【解决方案1】:

看起来你的 where 子句也是错误的。删除列别名 'b'

select [person number] 
from employee.[current pay as at evaluation date] 
where substring([Payroll Name],1,6) = 'DOV020')

【讨论】:

    【解决方案2】:

    您对查询结果的插入使用了错误的语法。对于这种类型的插入,您不需要 VALUES() 部分。

    作为旁注,您应该避免在您的INSERT SELECT 语句中使用SELECT *(或者,在这种情况下,a.*, c.*, d.*),因为您的临时表必须针对该插入语句进行正确设置工作,对目标或源表的任何更改都会导致它中断。

    你的陈述应该是这样的:

    Insert  #YourTempTable
            (List, Your, Columns, Here, ...)
    Select  a.*,
            b.[formal name],
            c.*,
            d.*
    From    selecthr20.employee.[career history]                                a
    Join    selecthr20.Employee.[Current Appointments As At Evaluation Date]    b   On  a.[appointment number] = b.[appointment number]
    Join    Employee.[BSK Changes in Selected Period]                           c   On  a.[career number] = c.[primary key number]
    Join    selecthr20.employee.[career history extra detail]                   d   On  a.[career number] = d.[career number]
                                                                                    And c.[primary key number] = d.[career number]
                                                                                    And c.[primary key name] = 'Career Number'
    Where   b.[person number] In 
    (
        Select  [person number] 
        From    employee.[current pay as at evaluation date] 
        Where   SubString([Payroll Name],1,6) = 'DOV020'
    )
    

    【讨论】:

      【解决方案3】:

      如果您不打算先创建临时表,请使用 select into。

      IF OBJECT_ID('TEMPDB.DBO.#TEMP') IS NOT NULL
          DROP TABLE #TEMP;
      BEGIN 
          SELECT 
              a.*, 
              b.[formal name], 
              c.*, 
              d.*
          INTO #TEMP
          FROM selecthr20.employee.[career history] a, 
              INNER JOIN selecthr20.Employee.[Current Appointments As At Evaluation Date] b
                  ON a.[appointment number] = b.[appointment number]  
              INNER JOIN Employee.[BSK Changes in Selected Period] c
                  ON a.[career number] = c.[primary key number]
              INNER JOIN selecthr20.employee.[career history extra detail] d
                  ON a.[career number] = d.[career number]
                      AND c.[primary key number] = d.[career number]
          where c.[primary key name] = 'Career Number'
          and b.[person number] in (select b.[person number] from employee.[current pay as at evaluation date] where substring([Payroll Name],1,6) = 'DOV020')
      END
      

      如果您想先创建表格然后插入,这里有一个模板可以让您了解结构。

      CREATE TABLE #TEMP
      (
          <YOURCOLUMNS>
      )
      
      INSERT INTO #TEMP
      (
          <YOURCOLUMNS>
      )
      SELECT 
          <YOURCOLUMNS>
      FROM selecthr20.employee.[career history] a, 
          INNER JOIN selecthr20.Employee.[Current Appointments As At Evaluation Date] b
              ON a.[appointment number] = b.[appointment number]  
          INNER JOIN Employee.[BSK Changes in Selected Period] c
              ON a.[career number] = c.[primary key number]
          INNER JOIN selecthr20.employee.[career history extra detail] d
              ON a.[career number] = d.[career number]
                  AND c.[primary key number] = d.[career number]
      where c.[primary key name] = 'Career Number'
      and b.[person number] in (select b.[person number] from employee.[current pay as at evaluation date] where substring([Payroll Name],1,6) = 'DOV020')
      

      【讨论】:

        猜你喜欢
        • 2015-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多