当前位置:  开发笔记 > 编程语言 > 正文

关键字'PROCEDURE'附近的语法不正确

如何解决《关键字'PROCEDURE'附近的语法不正确》经验,为你挑选了2个好方法。

在SQL Server中执行以下语句

IF  NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SP_CandidateRegistration]') AND type in (N'P', N'PC'))
BEGIN
CREATE PROCEDURE [dbo].[SP_CandidateRegistration]
    (
            @UserName VARCHAR(50),      
            @Password VARCHAR(50),
            @EmailID VARCHAR(50),
            @TestId int,
            @IsActiveUser INTEGER,      
            @USER_ID INTEGER OUTPUT
    )
    AS
    DECLARE @UserName VARCHAR(50)
    DECLARE @Password VARCHAR(50)
    DECLARE @EmailID VARCHAR(50)
    DECLARE @TestId int
    DECLARE @IsActiveUser INTEGER       
    DECLARE @USER_ID INTEGER 

    INSERT INTO [dbo].[IER_CandidateRegistration](User_Name, Password, EmailId, Test_Id, is_active )
    VALUES (@UserName, @Password, @EmailID,@TestId, @IsActiveUser) 
    select @USER_ID=@@identity
    RETURN
END
GO

在SQL Server 2008中执行后出错

Msg 156,Level 15,State 1,Line 3
关键字'PROCEDURE'附近的语法不正确.

Pranav.. 10

CREATE PROCEDURE声明不能与Transact-SQL单个批处理中的其他语句组合使用.所以,你必须这样做: -

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SP_CandidateRegistration]') AND type in (N'P', N'PC'))
DROP PROCEDURE [SP_CandidateRegistration]
GO

CREATE PROCEDURE [dbo].[SP_CandidateRegistration]
    (
            @UserName VARCHAR(50),      
            @Password VARCHAR(50),
            @EmailID VARCHAR(50),
            @TestId int,
            @IsActiveUser INTEGER,      
            @USER_ID INTEGER OUTPUT
    )
    AS

    INSERT INTO [dbo].[IER_CandidateRegistration](User_Name, Password, EmailId, Test_Id, is_active )
    VALUES (@UserName, @Password, @EmailID,@TestId, @IsActiveUser) 
    select @USER_ID=@@identity
    RETURN

GO

此外,您再次声明变量.



1> Pranav..:

CREATE PROCEDURE声明不能与Transact-SQL单个批处理中的其他语句组合使用.所以,你必须这样做: -

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SP_CandidateRegistration]') AND type in (N'P', N'PC'))
DROP PROCEDURE [SP_CandidateRegistration]
GO

CREATE PROCEDURE [dbo].[SP_CandidateRegistration]
    (
            @UserName VARCHAR(50),      
            @Password VARCHAR(50),
            @EmailID VARCHAR(50),
            @TestId int,
            @IsActiveUser INTEGER,      
            @USER_ID INTEGER OUTPUT
    )
    AS

    INSERT INTO [dbo].[IER_CandidateRegistration](User_Name, Password, EmailId, Test_Id, is_active )
    VALUES (@UserName, @Password, @EmailID,@TestId, @IsActiveUser) 
    select @USER_ID=@@identity
    RETURN

GO

此外,您再次声明变量.



2> Martin Smith..:

您可以CREATE在仅编译和执行的子批处理中运行IF NOT EXISTS.

您需要首先修复过程中的错误(为什么你想使用相同的名称来声明变量的参数也使用SCOPE_IDENTITY()没有@@IDENTITY),但像

IF  NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SP_CandidateRegistration]') AND type in (N'P', N'PC'))
BEGIN
EXEC('
     CREATE PROCEDURE [dbo].[SP_CandidateRegistration] (@UserName     VARCHAR(50),
                                                       @Password     VARCHAR(50),
                                                       @EmailID      VARCHAR(50),
                                                       @TestId       INT,
                                                       @IsActiveUser INTEGER,
                                                       @USER_ID      INTEGER OUTPUT)
    AS
        INSERT INTO [dbo].[IER_CandidateRegistration]
                    (User_Name,
                     Password,
                     EmailId,
                     Test_Id,
                     is_active)
        VALUES      (@UserName,
                     @Password,
                     @EmailID,
                     @TestId,
                     @IsActiveUser)

        SELECT @USER_ID = SCOPE_IDENTITY()

RETURN 
    ')
END

推荐阅读
依然-狠幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有