SQL Server SQL: Msg 156, Level 15, State 1 :Incorrect syntax near the keyword 'END'

wwwo4jvm  于 2023-05-21  发布在  其他
关注(0)|答案(4)|浏览(266)

Ok i have 2 questions 1) I've googled that error(in title) but I cant seem to get it to help me, my proc:

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

I've tied adding ) to NVARCHAR(255))) AS [Count] but still errors,

2)I need to get AS [Count] as a percentag, if anyone can help? That is my try code maybe i am on the right track.?

aelbi1ox

aelbi1ox1#

You're missing a close paren on your final subselect. Also, you're missing the 'S' in SET NOCOUNT. Updates below.

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
jvlzgdj9

jvlzgdj92#

Is it the missing 'S' on SET NOCOUNT ON after the initial begin perhaps?

xxb16uws

xxb16uws3#

It looks like your SELECT statement above your last last FROM statement has an un-closed parentheses. That would make the parser throw an error when it hits the END statement, since it still thinks it's working on the 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]
       ^

Whenever you get an incorrect Syntax error it's best to carefully go through your code and check that all statements are well formed and complete. Usually google can't help you, since it's very specific to your code. A good approach to finding the fault is to locate the point of the error ( END in this case) and work backwards. The cause of the error may be quite far above the actual point that the error is thrown, but it will be somewhere above the error. And reading backwards can help you catch simple mistakes.

1tuwyuhd

1tuwyuhd4#

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 @i=@i+1

End   

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

相关问题