Msg 156,Level 15,State 1,Procedure ShowDirectorateList,Line 739关键字' END'附近的语法不正确

时间:2015-12-02 09:59:00

标签: sql sql-server

我做了一个程序,我得到了很多错误信息,如

  

Msg 156,Level 15,State 1,Procedure ShowDirectorateList,Line 739   关键字' END'附近的语法不正确。

我的代码请帮助我解决它:

    --List ALL
    IF (@iType = 1 AND @iStatus=2)
    BEGIN
        WITH Allboth AS
        (
        SELECT ROW_NUMBER() OVER (ORDER BY [sName],[iDirectorateID]) AS RowNum
            ,[iDirectorateID]
            ,[sName] 
            ,[sAddress1] 
            ,[iPostCode] 
            ,[sLeadContact] 
            ,[iStatus] 
        FROM [dbo].[Directorate] Dir
            ,[dbo].[Contact] Con
            ,[dbo].[Address] Ad
            ,[dbo].[Detail] Det
        WHERE Dir.iContactID = Con.iContactID
        AND Con.iDetailID = Det.iDetailID
        AND Con.iAddressID = Ad.iAddressID
        ) 
        Insert Into @_FisrtTable
    END

我想从数据库中选择数据并放入@_FirstTable

    DECLARE @_FisrtTable Table
(
     [RowNum] int NULL
    ,[iDirectorateID] int NULL
    ,[sName] nvarchar(50) NULL
    ,[sAddress1] nvarchar(50) NULL
    ,[iPostCode] int NULL
    ,[sLeadContact] nvarchar(50) NULL
    ,[iStatus] int NULL
)

1 个答案:

答案 0 :(得分:0)

DECLARE @_FisrtTable TABLE (
    [RowNum] INT NULL,
    [iDirectorateID] INT NULL,
    [sName] NVARCHAR(50) NULL,
    [sAddress1] NVARCHAR(50) NULL,
    [iPostCode] INT NULL,
    [sLeadContact] NVARCHAR(50) NULL,
    [iStatus] INT NULL
)

IF (@iType = 1 AND @iStatus = 2) BEGIN

    ;WITH Allboth AS ( -- missing ";"
        SELECT
            ROW_NUMBER() OVER (ORDER BY [sName], [iDirectorateID]) AS RowNum,
            [iDirectorateID],
            [sName],
            [sAddress1],
            [iPostCode],
            [sLeadContact],
            [iStatus]
        FROM [dbo].[Directorate] Dir,
             [dbo].[Contact] Con,
             [dbo].[Address] Ad,
             [dbo].[Detail] Det
        WHERE Dir.iContactID = Con.iContactID
            AND Con.iDetailID = Det.iDetailID
            AND Con.iAddressID = Ad.iAddressID
    )
    INSERT INTO @_FisrtTable
    SELECT * FROM Allboth -- missing "SELECT" from CTE

END