如何在附近解释T-SQL错误不正确的语法

时间:2018-10-11 16:10:52

标签: sql-server tsql

关于行号和“批处理起始行”,我不太了解如何解释T-SQL错误消息。

当我尝试运行CREATE PROCEDURE时,出现以下错误:

  

消息102,级别15,状态1,过程LoadIndexersReports,第119行[批处理开始第9行]
  ')'附近的语法不正确。

     

消息102,级别15,状态1,过程LoadIndexersReports,第120行[批处理开始第9行]
  ')'附近的语法不正确。

当我转到119行时,这是我知道应该很好的代码,因为我将代码复制粘贴了,所以我决定注释掉整个代码段。

我仍然遇到相同的错误,但是119周围的所有行实际上都已被注释掉。

我该怎么解释?

关于T-SQL报告错误的怪异方式有任何线索吗?

我也许可以仔细研究一下,找出问题所在。

这不是问题。

我对线参考看起来多么愚蠢和毫无意义以及是否有办法解决这个问题感到困惑。

这是在第一行粘贴[USE]语句的完整代码:

USE [QtyCompare]
GO

/****** Object:  StoredProcedure [dbo].[LoadRecentlyIndexed]    Script Date: 10/10/2018 1:01:02 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[LoadIndexersReports] 
     @StartDate DATE, @DaysForward INT
AS
BEGIN
    SET NOCOUNT ON

    --  IF OBJECT_ID('dbo.TempRecentlyIndexedRaw'U') IS NOT NULL 
    DELETE FROM dbo.TempRecentlyIndexedRaw

    DECLARE @pLastModified DATETIME,
            @pPONO VARCHAR(50),
            @pProduct VARCHAR(100),
            @pQuantity VARCHAR(15),
            @pHeat VARCHAR(25),
            @pProduct_Description VARCHAR(250),
            @pSerial_Or_Lot VARCHAR(50),
            @pCoreData_IID_ssField14 INT,
            @pDocId INT,
            @pIndexers VARCHAR(200),
            @pAR_Alpha VARCHAR(50),
            @pValve BIT,
            @tempString VARCHAR(500),
            @pARs VARCHAR(250)   /*sorted ALPHABETICALLY, ASCENDING ... Note:  ABC234 > ABC1111, because its sorted ALPHABETICALLY */

    -- If Documents were deleted, remove them from your ocean
    DELETE IH
    FROM IndexHistory IH
    LEFT JOIN IndexedLineItems LI ON IH.Docid = LI.DocId
    WHERE LI.DocId IS NULL

    -- *************************************************************************************
    -- * B.
    -- PULL FROM RAW, CREATE TempRecentlyIndexed
    -- 
    -- *************************************************************************************

    IF OBJECT_ID('dbo.TempRecentlyIndexed', 'U') IS NOT NULL 
         DROP TABLE dbo.TempRecentlyIndexed;

    SELECT * 
    INTO TempRecentlyIndexed 
    FROM IndexHistory 
    WHERE LastModified >= @StartDate 
      AND [LastModified] <= CAST(DATEADD(dd, @DaysForward - 1, GETDATE()) AS DATE)

-- *************************************************************************************
    -- * C.  Generate your reports from TempRecentlyIndexed
    -- 
    -- 
    -- *************************************************************************************

    --lines summary
    SELECT 
        Indexers, SUM(LinesIndexed) AS [Total Lines Indexed], 
        SUM(LinesFactoringInValvesTTLHeats) AS [Total Lines Indexed Factoring In Valves (Multiple Heats)]  
    FROM
        (SELECT
             Indexers, AR_Alpha, 
             COUNT(*) AS LinesIndexed, SUM([Heat Items]) AS LinesFactoringInValvesTTLHeats 
         FROM
             (SELECT 
                  LastModified, PONO, Product, Quantity, 
                  IIF(Charindex('Valve Heats', Heat) > 0, CAST(Replace(Heat, ' Valve Heats', '') AS INT), 1) AS [Heat Items], 
                  DocId, Indexers, AR_Alpha 
              FROM
                  [dbo].[TempRecentlyIndexed] 
              WHERE
                  product IS NOT NULL) AS AAA
         GROUP BY 
             AR_Alpha, Indexers) PreviousQuery
    GROUP BY
        Indexers
    ORDER BY
        Indexers

    --documents
    Select Indexers, Count(*) as [Documents Indexed] from 
    (
    Select distinct Docid, Indexers from [dbo].[TempRecentlyIndexed] where Product is not null
    ) as AAA
    group by Indexers
    order by Indexers

    --lines detail
    select Indexers, AR_Alpha, Count(*) as [Lines Indexed], Sum([Heat Items]) as [Total Lines Indexed Factoring In Valves (Multiple Heats)] from 
    (
    SELECT LastModified, PONO, Product, Quantity, IIF(Charindex('Valve Heats',Heat)>0,Cast(Replace(Heat,' Valve Heats','') as int),1) as [Heat Items], DocId, Indexers, AR_Alpha from [dbo].[TempRecentlyIndexed] where product is not null
    ) as AAA
    group by AR_Alpha, Indexers 
    order by Indexers, AR_Alpha




-- *************************************************************************************
    -- * D.  Generate summaries for comparison with Arrivals data
    -- 
    -- 
    -- *************************************************************************************

    -- Recently Indexed Summary
--  IF OBJECT_ID('dbo.TempRecentlyIndexedSummary', 'U') IS NOT NULL 
--    DROP TABLE dbo.TempRecentlyIndexedSummary;
--
--  select LastModified, Product, sum(CASE WHEN ISNUMERIC(Quantity + 'e0') = 1 THEN CAST(Quantity AS float) ELSE 0 END) AS Quantity, Indexers, AR_Alpha, ARs
--  into TempRecentlyIndexedSummary
--  from TempRecentlyIndexed
--  where product is not null
--  Group by LastModified,Product,INdexers,AR_Alpha,ARs


-- *************************************************************************************
    -- * D2.  Load Related Records
    -- 
    -- 
    -- *************************************************************************************


    Delete from dbo.TempRelatedIndexedRaw
    Delete from dbo.TempRelatedIndexed
    SELECT distinct ARs into #ARList FROM [dbo].[IndexHistory] WHERE [LastModified] <= cast( dateadd(dd,@DaysForward-1,@StartDAte) as date) AND [LastModified] >= @StartDate)
    Select * into TempRelatedIndexed from IndexHistory left Join #ARList on IndexHistory.ARs = #ARList.ARs where Not([LastModified] <= cast( dateadd(dd,@DaysForward-1,@StartDAte) as date) AND [LastModified] >= @StartDate))



    -- Summary to include both recently indexed and related indexed
    IF OBJECT_ID('dbo.TempIndexedSummary', 'U') IS NOT NULL 
      DROP TABLE dbo.TempIndexedSummary;

    Select Product, Sum(Quantity) as Quantity, AR_Alpha, ARs into TempIndexedSummary from
    (Select * from TempRecentlyIndexedSummary
    Union All
    Select * from TempRelatedIndexedSummary) CombinedSummaries
    group by Product, AR_Alpha, ARs 


    --exec [dbo].[LoadRecentlyIndexedARSets]


    --Arrival Summary
    IF OBJECT_ID('dbo.TempArrivalSummary', 'U') IS NOT NULL 
      DROP TABLE dbo.TempArrivalSummary;

    Select AProdNo as Product, Sum(QTY) as Quantity, ARs into TempArrivalSummary from
    (
    select RA.*, ARSets.[Set] as ARs from 
    (Select * from Raw_Arrival Union All Select * from Arrivals_2017 ) RA
    left join [IndexHistoryARSets] ARSets on RA.ARsupp = ARSets.AR 
    where RA.TrueBOSTK = 'STK' and (ARSets.[Set] is not null)
    ) Arrivals
    group by AProdNo, ARs


    -- *************************************************************************************
    -- * E.  Generate DISCREPENCY report based on summary
    -- 
    -- 
    -- *************************************************************************************

    --Generate Discrepency Report
    select Coalesce(TAS.Product,'<No Data>') as Arrival_Product, Coalesce(TAS.ARs,'<No Data>') Arrival_ARs, Coalesce(TAS.Quantity ,0) as Arrival_Qty, Coalesce(TIS.Quantity  ,0) as Indexed_Qty, Coalesce(TIS.Product   ,'<No Data>') as Ndx_Product, Coalesce(TIS.ARs ,'<No Data>') as Ndx_ARs,
    Coalesce((Select top 1 Indexers from TempRecentlyIndexed TRI where TRI.ARs = TIS.ARs), '<No Data>')
    from
    TempArrivalSummary TAS FULL OUTER JOIN TempIndexedSummary TIS
    on TAS.Product = TIS.Product and TAS.ARs = TIS.ARs


    -- *************************************************************************************
    -- * E.  Generate Pivot table data
    -- 
    -- 
    -- *************************************************************************************

    --THIS IS YOUR PIVOT SET FOR HARSHAL
    Select PivotMain.*, Coalesce(PivotAdditional.Country,'Unknown') as Country, Coalesce(PivotAdditional.[Assigned Indexer], 'Undefined') as [Assigned Indexer], Replace(Coalesce(PivotAdditional.Automated,''),'X','YES') as Automated, Coalesce(PivotAdditional.Domestic,'') as Domestic, PivotAdditional.[Line Items Recieved To Date] as [Relative Volume] from
    (Select Indexers, 1 as Lines, iif(Charindex('Valve Heats', Coalesce(Heat,''), 0)>0,Cast(RTrim(LTrim(Replace(Heat,'Valve Heats',''))) as int),1) as [Lines Factoring In Valves], Docid, LastModified, AR_Alpha as Supplier, iif(Charindex('.', AR_Alpha)=2,RIGHT(ar_ALPHA,LEN(ar_aLPHA)-2),AR_Alpha) as Suffix, ARs from [dbo].[TempRecentlyIndexed]) PivotMain
    left join Suffix PivotAdditional on (PivotMain.Supplier = PivotAdditional.Supplier and PivotMain.Suffix = PivotAdditional.Suffix)



End
GO

2 个答案:

答案 0 :(得分:3)

有错误的行号在SSMS查询窗口和SQL解释器报告的内容之间经常不匹配。解释器报告相对于批处理中正在执行的语句的行号。如果您有多个批次(通常用GO隔开),则例外中的行号将与查询窗口中显示的行号不符。

在SSMS中,您可以在“结果”窗格中双击错误消息,然后SSMS会将光标放在引起错误的语句上。

在您的示例中,引发错误的行是这样的:
SELECT distinct ARs into #ARList FROM [dbo].[IndexHistory] WHERE [LastModified] <= cast( dateadd(dd,@DaysForward-1,@StartDAte) as date) AND [LastModified] >= @StartDate)

此语句的)过多。最后删除一个以修复该行中的语法。还要注意,下一条语句也有同样的问题。您需要修复这两个语句。

答案 1 :(得分:1)

当我们有很多代码时,可能在这些情况下,我们可能会出现sintax错误。

更简单的获取错误的方法是双击错误,无论行号如何,SQL Management都会带您到必须检查的行。

enter image description here