8

在 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 中执行后出错

消息 156,级别 15,状态 1,第 3 行
关键字“PROCEDURE”附近的语法不正确。

4

3 回答 3

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

此外,您再次声明变量。

于 2013-05-24T07:19:14.423 回答
9

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

您将需要首先修复过程中的错误(为什么要声明与参数同名的变量也使用SCOPE_IDENTITY()not @@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
于 2013-05-24T07:20:34.020 回答
1

试试这个:

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SP_CandidateRegistration]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[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
    BEGIN
    DECLARE @UserName1 VARCHAR(50)
    DECLARE @Password1 VARCHAR(50)
    DECLARE @EmailID1 VARCHAR(50)
    DECLARE @TestId1 int
    DECLARE @IsActiveUser1 INTEGER       
    DECLARE @USER_ID1 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
于 2013-05-24T07:32:46.510 回答