我正在尝试从select语句中插入值,我收到了错误
查询表达式中的语法错误'选择C.ClientNo作为ClientNo'
我的查询
Insert into [TempJobRecovery] (ClientNo,ClientName,TaxInvoiceNo,FeesforPS,TaxInvoiceDate,Partner,DeptNo,JobCode,TotalHour,TotalCost,Recovery,Status,PINO) Values
(Select C.ClientNo as ClientNo,
C.ClientName as ClientName,
C.taxinvoiceno as TaxInvoiceNo,
C.feesforps AS FeesforPS,
C.taxinvoicedte as TaxInvoiceDate,
C.Partner as Partner,
C.DeptNo as DeptNo,
C.JobCode as JobCode,
Sum(T.hrs) AS TotalHour,
Sum(T.hrs*T.corate) AS TotalCost,
(FeesforPS/TotalCost) as Recovery,
C.Status as Status,
C.PINO as PINO
FROM Clienttaxinvoice AS C
LEFT JOIN TimeSys AS T ON C.jobcode = T.jobcode
WHERE ((C.ClientNo)=[T].[cntno]) and C.Taxinvoicedte>=#01/31/2017# and C.Taxinvoicedte<=#01/31/2018#GROUP BY SerialNo,ClientNo,ClientName,TaxInvoiceNo,FeesforPS,C.taxinvoicedte,Partner,DeptNo,C.JobCode,Status,PINO)
我的查询有什么问题?请让我知道。
答案 0 :(得分:3)
INSERT INTO
与SELECT
查询结合使用时,不会使用VALUES
。相反,VALUES
仅用于插入文字元组数据。尝试删除VALUES
:
INSERT INTO [TempJobRecovery] (ClientNo, ClientName, TaxInvoiceNo, FeesforPS,
TaxInvoiceDate, Partner, DeptNo, JobCode, TotalHour, TotalCost, Recovery,
Status, PINO)
SELECT
C.ClientNo,
C.ClientName,
C.taxinvoiceno,
C.feesforps,
C.taxinvoicedte,
C.Partner,
C.DeptNo,
C.JobCode,
SUM(T.hrs),
SUM(T.hrs*T.corate),
(FeesforPS / TotalCost),
C.Status,
C.PINO
FROM Clienttaxinvoice AS C
LEFT JOIN TimeSys AS T
ON C.jobcode = T.jobcode
WHERE
C.ClientNo = [T].[cntno] AND
C.Taxinvoicedte >= #01/31/2017# AND
C.Taxinvoicedte <= #01/31/2018#
GROUP BY
SerialNo,
ClientNo,
ClientName,
TaxInvoiceNo,
FeesforPS,
C.taxinvoicedte,
Partner,
DeptNo,
C.JobCode,
Status,
PINO
请注意,我从上面的select语句中删除了别名。没有必要使用别名,因为列名已经固定在插入的目标表中。