Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 存储过程错误-语法不正确_Sql_Sql Server_Stored Procedures - Fatal编程技术网

Sql 存储过程错误-语法不正确

Sql 存储过程错误-语法不正确,sql,sql-server,stored-procedures,Sql,Sql Server,Stored Procedures,我正在处理我的第一个存储过程,它在下面的else中显示了一个错误。如果有人知道我如何纠正错误,我将不胜感激。我试过上网,我想这可能与我的开始/结束和如果/否则定位有关。不过,我很难找到类似的例子 SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE procedure [dbo].[SPName] @userid NVARCHAR(51) AS SET NOCOUNT ON DECLARE @return_

我正在处理我的第一个存储过程,它在下面的else中显示了一个错误。如果有人知道我如何纠正错误,我将不胜感激。我试过上网,我想这可能与我的开始/结束和如果/否则定位有关。不过,我很难找到类似的例子

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE procedure [dbo].[SPName]
    @userid NVARCHAR(51)
AS 
    SET NOCOUNT ON

    DECLARE @return_status  INT

    IF @userid IS NULL OR @userid = 0
    BEGIN
        --fail
        SET @return_status = 1
    END

    IF  @return_status <> 1
    BEGIN
        IF EXISTS (SELECT login
                   FROM dbo.user_table
                   WHERE (@userid = login))
        --continue because its a good user
        ELSE  
            -- this is where it doesn't like the else
            SET @return_status = 1
    END

    BEGIN
        UPDATE dbo.U_MEMBER
        SET dbo.U_MEMBER.inac = 1,
            dbo.U_MEMBER.reg = @userid
        FROM dbo.U_MEMBER
        WHERE U_MEMBER.VF = 'REV'
    END

    RETURN 0
如果IF语句等于true,则与之无关:

IF  @return_status <> 1
BEGIN
    IF EXISTS(select login from dbo.user_table where  (@userid=login))
        --continue because its a good user
    ELSE  --this is where it doesn't like the else
        set @return_status = 1
    END
...
否则,如果要同时使用if语句的正确和错误结果:

IF  @return_status <> 1
BEGIN
    IF EXISTS(select login from dbo.user_table where  (@userid=login))
    BEGIN
        -- Do something with the true outcome
    END
    ELSE
    BEGIN
        set @return_status = 1
    END
...
如果IF语句等于true,则与之无关:

IF  @return_status <> 1
BEGIN
    IF EXISTS(select login from dbo.user_table where  (@userid=login))
        --continue because its a good user
    ELSE  --this is where it doesn't like the else
        set @return_status = 1
    END
...
否则,如果要同时使用if语句的正确和错误结果:

IF  @return_status <> 1
BEGIN
    IF EXISTS(select login from dbo.user_table where  (@userid=login))
    BEGIN
        -- Do something with the true outcome
    END
    ELSE
    BEGIN
        set @return_status = 1
    END
...

初始化@return\u状态变量,例如:

DECLARE @return_status  INT

--Initialize the variable with the expected value
SET @return_status = 0

IF @userid IS NULL OR @userid = 0
BEGIN
--fail
    SET @return_status=1
END

初始化@return\u状态变量,例如:

DECLARE @return_status  INT

--Initialize the variable with the expected value
SET @return_status = 0

IF @userid IS NULL OR @userid = 0
BEGIN
--fail
    SET @return_status=1
END

在那组特定批次的第27行,你开始一个IF,但如果它是真的,你就没有事情做。你直接去别的地方。如果IF的计算结果为true,则需要执行某些操作。另一方面,将不推荐使用列的3部分命名,或从SQL Server中删除。理想情况下,在引用列时,坚持使用两部分命名和别名;那么当它发生时,您就不会被发现。另外,设置@return\u status=1的目的是让SP失败,因为它不会。事实上,第二次设置时,它什么也没有实现,因为它之后从未被引用过。似乎您最好使用第一个IF的THROW和一条自定义错误消息。U_成员和user_表之间的外键将不需要进行存在性检查。在这个模式和代码中有很多地方需要改进。建议对最佳实践进行一些调查。在这组特定批次的第27行,您启动一个IF,但如果它是真的,您就没有什么事情要做了。你直接去别的地方。如果IF的计算结果为true,则需要执行某些操作。另一方面,将不推荐使用列的3部分命名,或从SQL Server中删除。理想情况下,在引用列时,坚持使用两部分命名和别名;那么当它发生时,您就不会被发现。另外,设置@return\u status=1的目的是让SP失败,因为它不会。事实上,第二次设置时,它什么也没有实现,因为它之后从未被引用过。似乎您最好使用第一个IF的THROW和一条自定义错误消息。U_成员和user_表之间的外键将不需要进行存在性检查。在这个模式和代码中有很多地方需要改进。建议对最佳实践进行一些调查。这很有帮助,谢谢。这不是我犯错误的原因。我将接受另一个答案,并投票支持你的答案,认为它有帮助。这很有帮助,谢谢。这不是我犯错误的原因。我将接受另一个答案,并投票支持你的答案,认为它有帮助。