2015-05-12 195 views
-1

我的代碼在L42(from子句)上返回語法錯誤。我看了看,對我來說,語法看起來正確的爲FROMSQL Server 2008:FROM子句語法錯誤

代碼的頂部創建臨時表'子產品'。然後我想從數據庫表中提取數據&從新的臨時表中提取相關詳細信息。

任何想法?

drop table ##subproducts; 

SELECT 
    F1.[OrderNo] 
    ,F1.[OrderSeqNo] 
    ,F1.[OrderLineNo] 
    ,sum(F1.[LineCost]) as LineCost 
    ,sum(F1.[NetCost]) as NetCost 
    ,sum(F1.[OrderNet]) as OrderNet 
INTO 
    ##subproducts 
FROM 
    [UFPData].[dbo].[SalesStats] F1 
WHERE 
    F1.MainProd = 'S' 
GROUP BY 
    F1.OrderNo, F1.OrderSeqNo, F1.OrderLineNo; 

SELECT * 
FROM SalesStats F1 
LEFT OUTER JOIN ##subproducts F2 ON F1.OrderNo = F2.OrderNo 
           AND F1.OrderSeqNo = F2.Orderseqno 
           AND F1.OrderLineNo = F2.OrderLineNo 
WHERE YEAR(InvDate) = 2015 
    AND MONTH(InvDate) = 5 
    AND CustNo = 100382 
    AND MainProd = 'Y'; 

SELECT 
    ContractInfo.CONTRNO, 
    ContractInfo.CONTRDESC, 
    repcode, 
    RepName, 
    PriceAgreement, 
    ordercycle, 
    SalesStats.CustNo, 
    Customer.CustName, 
    SalesStats.InvDate, 
    SalesStats.InvoiceNo, 
    salesstats.ProdCode, 
    Price as ContractPrice, 
    subproduct.LineCost as LineCost, 
    subproduct.NetCost as NetCost, 
    subproduct.OrderNet as OrderNet, 
    contractinfo.NETCOST as BidPrice, 
    contractinfo.NETCOST* SUM(quantity) as BidCost, 
FROM 
    UFPData.dbo.SalesStats 
RIGHT OUTER JOIN 
    ##subproducts ON UFPData.dbo.SalesStats.ProdCode = ##subproducts.ProdCode, 
    UFPData.dbo.SalesStats 
LEFT OUTER JOIN 
    UFPData.dbo.Customer ON UFPData.dbo.SalesStats.CustNo = UFPData.dbo.Customer.CustNo, UFPData.dbo.SalesStats 
LEFT OUTER JOIN 
    Pricing.dbo.InvManCen ON UFPData.dbo.SalesStats.ProdCode = Pricing.dbo.invmancen.UFPCODE, UFPData.dbo.SalesStats 
RIGHT OUTER JOIN 
    UFPData.dbo.ContractInfo ON UFPData.dbo.Customer.CustNo = UFPData.dbo.ContractInfo.CUSTNO 
          AND UFPData.dbo.salesstats.prodcode = UFPData.dbo.contractinfo.prodcode, 
WHERE 
    invdate BETWEEN '2015-05-01' and '2015-05-31' 
    AND TeamCode IN ('tm1','tm2','tm3') 
    AND ContractInfo.CONTRNO IN ('1500','1502','1503','1504','1505','1506','701','702','703','705','141', 
'712', '713', '714', '715', '716', '717', '718', '719', '730', '731', '732') or CONTRNO between '3000' and '3049') 
--and left(ordercycle,1) <> 'c' 
GROUP BY 
    ContractInfo.CONTRNO, 
    repcode, 
    RepName, 
    SalesStats.CustNo, 
    Customer.CustName, 
    SalesStats.InvDate, 
    salesstats.ProdCode, 
    Price, 
    contractinfo.NETCOST, 
    SalesStats.InvoiceNo, 
    InvManCen.PS98, 
    ContractInfo.CONTRDESC, 
    ordercycle, 
    PriceAgreement, 

case 
when invdate > ('20' + LEFT(ENDDATE,2)+ '-'+ SUBSTRING(cast(ENDDATE as varchar(6)),3,2)+ '-'+ SUBSTRING(cast(ENDDATE as varchar(6)),5,2)) then 'Expired' else 'Live' end; 

提前感謝

+0

如果##副產品不存在呢? –

+7

是的,您在'FROM'之前有一個額外的',':BidCost,FROM' – Lamak

回答

1

這不正確的看向我:

FROM 
UFPData.dbo.SalesStats right outer join ##subproducts on UFPData.dbo.SalesStats.ProdCode = ##subproducts.ProdCode, 
UFPData.dbo.SalesStats left outer join UFPData.dbo.Customer on UFPData.dbo.SalesStats.CustNo = UFPData.dbo.Customer.CustNo, 
UFPData.dbo.SalesStats left outer join Pricing.dbo.InvManCen on UFPData.dbo.SalesStats.ProdCode = Pricing.dbo.invmancen.UFPCODE, 
UFPData.dbo.SalesStats right outer join UFPData.dbo.ContractInfo on UFPData.dbo.Customer.CustNo = UFPData.dbo.ContractInfo.CUSTNO and UFPData.dbo.salesstats.prodcode = UFPData.dbo.contractinfo.prodcode, 

通常格式是這樣的:

FROM 
    TableName 
    INNER JOIN SomeOtherTable... 
    INNER JOIN AnotherTable... 

注意,第一表提到一次,不是每次加入之前。在每次連接之後,您似乎也會錯誤地使用逗號,這在語法上不正確。我想你想要的是:

FROM 
UFPData.dbo.SalesStats 
right outer join ##subproducts on UFPData.dbo.SalesStats.ProdCode = ##subproducts.ProdCode 
left outer join UFPData.dbo.Customer on UFPData.dbo.SalesStats.CustNo = UFPData.dbo.Customer.CustNo 
left outer join Pricing.dbo.InvManCen on UFPData.dbo.SalesStats.ProdCode = Pricing.dbo.invmancen.UFPCODE 
right outer join UFPData.dbo.ContractInfo on UFPData.dbo.Customer.CustNo = UFPData.dbo.ContractInfo.CUSTNO and UFPData.dbo.salesstats.prodcode = UFPData.dbo.contractinfo.prodcode 

在整個查詢中可能會有更多的語法錯誤。我建議將代碼格式化得更乾淨一些,以便更容易地找到它們。例如,對比上面是這樣的:

FROM 
    UFPData.dbo.SalesStats 
    RIGHT OUTER JOIN ##subproducts 
     ON UFPData.dbo.SalesStats.ProdCode = ##subproducts.ProdCode 
    LEFT OUTER JOIN UFPData.dbo.Customer 
     ON UFPData.dbo.SalesStats.CustNo = UFPData.dbo.Customer.CustNo 
    LEFT OUTER JOIN Pricing.dbo.InvManCen 
     ON UFPData.dbo.SalesStats.ProdCode = Pricing.dbo.invmancen.UFPCODE 
    RIGHT OUTER JOIN UFPData.dbo.ContractInfo 
     ON UFPData.dbo.Customer.CustNo = UFPData.dbo.ContractInfo.CUSTNO 
     AND UFPData.dbo.salesstats.prodcode = UFPData.dbo.contractinfo.prodcode 

這是一個小的變化,往往個人喜好的問題,但良好的格式化代碼帶來了許多優勢。較少的水平滾動使得查看相關代碼變得更加容易,特定關鍵字和子句的換行符使得更容易發現錯誤的逗號等。

0

下面是我認爲應該是有效的語法提供,請嘗試它並將其與原來的比較,因爲有多個錯誤:

DROP TABLE ##subproducts; 

SELECT F1.[OrderNo] , 
     F1.[OrderSeqNo] , 
     F1.[OrderLineNo] , 
     SUM(F1.[LineCost]) AS LineCost , 
     SUM(F1.[NetCost]) AS NetCost , 
     SUM(F1.[OrderNet]) AS OrderNet 
INTO ##subproducts 
FROM [UFPData].[dbo].[SalesStats] F1 
WHERE F1.MainProd = 'S' 
GROUP BY F1.OrderNo , 
     F1.OrderSeqNo , 
     F1.OrderLineNo; 

SELECT * 
FROM SalesStats F1 
     LEFT OUTER JOIN ##subproducts F2 ON F1.OrderNo = F2.OrderNo 
              AND F1.OrderSeqNo = F2.Orderseqno 
              AND F1.OrderLineNo = F2.OrderLineNo 
WHERE YEAR(InvDate) = 2015 
     AND MONTH(InvDate) = 5 
     AND CustNo = 100382 
     AND MainProd = 'Y'; 

SELECT ContractInfo.CONTRNO , 
     ContractInfo.CONTRDESC , 
     repcode , 
     RepName , 
     PriceAgreement , 
     ordercycle , 
     SalesStats.CustNo , 
     Customer.CustName , 
     SalesStats.InvDate , 
     SalesStats.InvoiceNo , 
     SalesStats.ProdCode , 
     Price AS ContractPrice , 
     subproduct.LineCost AS LineCost , 
     subproduct.NetCost AS NetCost , 
     subproduct.OrderNet AS OrderNet , 
     ContractInfo.NetCost AS BidPrice , 
     ContractInfo.NetCost * SUM(quantity) AS BidCost 
FROM UFPData.dbo.SalesStats 
     RIGHT OUTER JOIN ##subproducts ON UFPData.dbo.SalesStats.ProdCode = ##subproducts.ProdCode 
     LEFT OUTER JOIN UFPData.dbo.Customer ON UFPData.dbo.SalesStats.CustNo = UFPData.dbo.Customer.CustNo 
     LEFT OUTER JOIN Pricing.dbo.InvManCen ON UFPData.dbo.SalesStats.ProdCode = Pricing.dbo.invmancen.UFPCODE 
     RIGHT OUTER JOIN UFPData.dbo.ContractInfo ON UFPData.dbo.Customer.CustNo = UFPData.dbo.ContractInfo.CUSTNO 
                AND UFPData.dbo.salesstats.prodcode = UFPData.dbo.contractinfo.prodcode 
WHERE invdate BETWEEN '2015-05-01' AND '2015-05-31' 
     AND TeamCode IN ('tm1', 'tm2', 'tm3') 
     AND (ContractInfo.CONTRNO IN ('1500', '1502', '1503', '1504', '1505', 
             '1506', '701', '702', '703', '705', 
             '141', '712', '713', '714', '715', '716', 
             '717', '718', '719', '730', '731', '732') 
     OR CONTRNO BETWEEN '3000' AND '3049') 
GROUP BY ContractInfo.CONTRNO , 
     repcode , 
     RepName , 
     SalesStats.CustNo , 
     Customer.CustName , 
     SalesStats.InvDate , 
     SalesStats.ProdCode , 
     Price , 
     ContractInfo.NetCost , 
     SalesStats.InvoiceNo , 
     InvManCen.PS98 , 
     ContractInfo.CONTRDESC , 
     ordercycle , 
     PriceAgreement , 
     CASE WHEN invdate > ('20' + LEFT(ENDDATE, 2) + '-' 
           + SUBSTRING(CAST(ENDDATE AS VARCHAR(6)), 3, 2) 
           + '-' + SUBSTRING(CAST(ENDDATE AS VARCHAR(6)), 5, 
               2)) THEN 'Expired' 
      ELSE 'Live' 
     END; 
+0

是的,我很新。這部分代碼是我從之前完成這項工作的人那裏拿來的,還有一些代碼是我被要求放在一起的。這些內容大部分都在我的頭上。我認爲它運行的問題是,在FROM語句之前的行結尾(我認爲是42)。當我使用AdventureWorks DB和我的SQL書的時候,我已經明白了。 – IainW

+0

@IainW你現在有沒有工作,或者你有進一步的疑問? – Tanner