简体   繁体   English

使用子查询向临时表SQL Server 2008中插入值时出现问题

[英]Issue inserting values with a subquery into temporary table SQL Server 2008

Trying to insert values into a temporary table using SQL Server 2008, getting: 尝试使用SQL Server 2008将值插入临时表中,获得:

Msg 116, Level 16, State 1, Procedure Test_temp_table, Line 274 消息116,级别16,状态1,过程Test_temp_table,行274
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. 如果未使用EXISTS引入子查询,则只能在选择列表中指定一个表达式。

With this query: 使用此查询:

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')))

If you are not going to create the temp table first, use select into. 如果您不打算首先创建临时表,请使用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

If you want to create the table first and then insert, here is a template to give you an idea of the structure. 如果要先创建表然后插入,这里是一个模板,可让您了解结构。

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')

You're using the wrong syntax for an insert from a query result. 您对查询结果的插入使用了错误的语法。 For this type of insert, you don't need the VALUES() portion. 对于这种类型的插入,您不需要VALUES()部分。

As a side-note, you should avoid using SELECT * (or, in this case, a.*, c.*, d.* ) in your INSERT SELECT statement, as your temp table would have to be set up just right for that insert statement to work, and any changes to the target or source tables would cause that to break. 附带说明一下,您应该避免在INSERT SELECT语句中使用SELECT * (或者在这种情况下, a.*, c.*, d.* ),因为临时表必须恰好适合该插入语句起作用,并且对目标表或源表的任何更改都将导致该中断。

Your statement should look something like this: 您的声明应如下所示:

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'
)

Looks like your where clause is also wrong. 看起来您的where子句也是错误的。 Remove the column alias 'b' 删除列别名“ b”

ie

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM