2014-01-08 156 views
0

好的我有2個問題1)我用google搜索了這個錯誤(標題)但我似乎無法幫助我,我PROC:SQL:Msg 156,Level 15,State 1:關鍵字'END'附近的語法不正確

CREATE PROCEDURE [dbo].[p_Target] 
AS 
BEGIN 
ET NOCOUNT ON 

    CREATE TABLE #tmp1 
    (
     AUD_ID BIGINT, 
     RowCounter BIGINT, 
     DistinctCounter BIGINT, 
     NACounter BIGINT, 
     Total BIGINT, 
     [Status] VARCHAR(MAX) 
    ) 
    INSERT INTO #tmp1 EXEC [p_GetCompleteIncompleteNaOverviewSCORE] 
DECLARE @Total AS INT 

    SELECT @Total = COUNT(*) 
    FROM (
    SELECT CASE WHEN CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,t2.AUD_TargetDate), 101)) < CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,GETDATE()), 101)) 
      THEN 'Over Due: ' 
      ELSE 'On Time: ' END AS [Target Status] 
      FROM #tmp1 t1 INNER JOIN dbo.Audit t2 
      ON t1.AUD_ID = t2.AUD_ID 
      WHERE t1.[Status] = 'Open') DER 

    SELECT ([Target Status] + '' + CAST(COUNT(*) AS NVARCHAR(255)) + ' of ' + CAST(@Total AS NVARCHAR(255))) AS TargetStatus, (SELECT COUNT(*)) * 100/(SELECT ([Target Status] + '' + CAST(COUNT(*) AS NVARCHAR(255))) AS [Count] 
    FROM (
    SELECT CASE WHEN CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,t2.AUD_TargetDate), 101)) < CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,GETDATE()), 101)) 
      THEN 'Over Due: ' 
      ELSE 'On Time: ' END AS [Target Status] 
      FROM #tmp1 t1 INNER JOIN dbo.Audit t2 
      ON t1.AUD_ID = t2.AUD_ID 
      WHERE t1.[Status] = 'Open') DER 
    GROUP BY [Target Status] 
    END 
GO 

我綁加入),以NVARCHAR(255))) AS [Count]但仍然錯誤, 2)我需要得到AS [Count]作爲percentag,如果有人能幫助?這是我的嘗試代碼,也許我在正確的軌道上。?

+1

'ET NOCOUNT ON'缺少'S'是一個錯字? – Satpal

+0

這是粘貼錯誤。 – inzefinite

回答

2

你錯過了最後的子選擇。另外,您在SET NOCOUNT中缺少'S'。下面更新。

CREATE PROCEDURE [dbo].[p_Target] 
AS 
BEGIN 
SET NOCOUNT ON 

    CREATE TABLE #tmp1 
    (
     AUD_ID BIGINT, 
     RowCounter BIGINT, 
     DistinctCounter BIGINT, 
     NACounter BIGINT, 
     Total BIGINT, 
     [Status] VARCHAR(MAX) 
    ) 
    INSERT INTO #tmp1 EXEC [p_GetCompleteIncompleteNaOverviewSCORE] 
DECLARE @Total AS INT 

    SELECT @Total = COUNT(*) 
    FROM (
    SELECT CASE WHEN CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,t2.AUD_TargetDate), 101)) < CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,GETDATE()), 101)) 
      THEN 'Over Due: ' 
      ELSE 'On Time: ' END AS [Target Status] 
      FROM #tmp1 t1 INNER JOIN dbo.Audit t2 
      ON t1.AUD_ID = t2.AUD_ID 
      WHERE t1.[Status] = 'Open') DER 

    SELECT ([Target Status] + '' + CAST(COUNT(*) AS NVARCHAR(255)) + ' of ' + CAST(@Total AS NVARCHAR(255))) AS TargetStatus, (SELECT COUNT(*)) * 100/(SELECT ([Target Status] + '' + CAST(COUNT(*) AS NVARCHAR(255))) AS [Count] 
    FROM (
    SELECT CASE WHEN CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,t2.AUD_TargetDate), 101)) < CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,GETDATE()), 101)) 
      THEN 'Over Due: ' 
      ELSE 'On Time: ' END AS [Target Status] 
      FROM #tmp1 t1 INNER JOIN dbo.Audit t2 
      ON t1.AUD_ID = t2.AUD_ID 
      WHERE t1.[Status] = 'Open') DER 
    GROUP BY [Target Status] 
    ) 
    END 
GO 
0

難道在最初的開始之後SET NOCOUNT ON上缺少'S'嗎?

+0

這是一個粘貼錯誤 – inzefinite

0

它看起來像你的SELECT聲明上面你最後的最後FROM聲明有一個非閉合的括號。這會使解析器在遇到END聲明時發出錯誤,因爲它仍認爲它在SELECT上工作。

SELECT ([Target Status] + '' + CAST(COUNT(*) AS NVARCHAR(255)) + ' of ' + CAST(@Total AS NVARCHAR(255))) AS TargetStatus, (SELECT COUNT(*)) * 100/(SELECT ([Target Status] + '' + CAST(COUNT(*) AS NVARCHAR(255))) AS [Count] 
    ^

每當你得到一個incorrect Syntax錯誤,最好仔細地去通過您的代碼,並檢查所有的語句都很好形成,完整。通常谷歌無法幫助你,因爲它是非常特定於你的代碼。尋找故障的一個好方法是定位錯誤點(在這種情況下爲END)並向後工作。錯誤的原因可能遠遠超出錯誤發生的實際點,但它會位於錯誤之上的某個位置。向後閱讀可以幫助你發現簡單的錯誤。

0
Create Table TblAccount (UserName nvarchar(20) Primary Key,UserPwd varchar(20) not null) 

Declare @i int 
Set @i=1 

While @i<=10 
I have one question 
BEGIN 
    Insert INTO TblAccount Values('Seng Vitou'+CAST(@i AS VARCHar(2)),REVERSE('pwd'+CAST(@i as varchar(2)) 

    SET @[email protected]+1 

End 

Msg 156, Level 15, State 1, Line 11 
Incorrect syntax near the keyword 'SET'. 
相關問題