[英]Issue inserting values with a subquery into temporary table 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')))
如果您不打算首先創建臨時表,請使用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')
您對查詢結果的插入使用了錯誤的語法。 對於這種類型的插入,您不需要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'
)
看起來您的where子句也是錯誤的。 刪除列別名“ b”
即
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.