2015-12-14 79 views
0

嘗試使用SQL Server 2008中插入值到一個臨時表,得到:問題插入值與子查詢到臨時表中的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'))) 
+0

您似乎使用錯誤的語法。 'INSERT INTO ... SELECT'根本不使用'VALUES()'表達式。它只是'INSERT INTO #TempTable SELECT ....'。 –

+0

[踢壞的習慣:使用舊式JOIN](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-old-style-joins。 aspx) - 在ANSI - ** 92 ** SQL標準(**超過20年前的**)中,舊式*逗號分隔的表*樣式列表被替換爲* proper * ANSI'JOIN'語法它的使用是不鼓勵的 –

回答

1

如果你不打算先創建臨時表,使用選擇進入。

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

看起來你的where子句也是錯誤的。刪除列別名「B」

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

您使用了錯誤的語法從查詢結果的插入。對於這種插入類型,您不需要VALUES()部分。

作爲一個側面說明,你應該避免使用SELECT *(或者,在這種情況下,a.*, c.*, d.*)在INSERT SELECT聲明,因爲你的臨時表將不得不被設置得恰到好處對於插入語句工作,任何目標或源表的更改會導致該問題中斷。

你的說法應該是這個樣子:

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