SQL:消息156,级别15,状态1:关键字“END”附近的语法不正确

时间:2014-01-08 13:22:24

标签: sql-server tsql

好的我有2个问题1)我用谷歌搜索了这个错误(标题中)但我似乎无法帮助我,我的过程:

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

我已经加入了NVARCHAR(255))) AS [Count],但仍然是错误,

2)如果有人能提供帮助,我需要AS [Count]作为百分比吗?这是我的尝试代码,也许我在正确的轨道上。?

4 个答案:

答案 0 :(得分:4)

你错过了最后一个subselect的密切关注。此外,你错过了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

答案 1 :(得分:0)

在初始开始之后SET NOCOUNT ON上是否缺少'S'?

答案 2 :(得分: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)并向后工作。错误的原因可能远远高于引发错误的实际点,但它将高于错误。向后阅读可以帮助你捕捉到简单的错误。

答案 3 :(得分: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 @i=@i+1

End   

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