gpt4 book ai didi

sql - SQL Server 中的多个单独的 IF 条件

转载 作者:行者123 更新时间:2023-12-02 03:28:09 24 4
gpt4 key购买 nike

我有多个IF在我的存储过程中彼此独立的语句。但由于某种原因,它们相互嵌套,就好像它们是一个大 if 语句的一部分

ELSE IF(SOMETHNGZ)
BEGIN
IF(SOMETHINGY)
BEGIN..END
ELSE IF (SOMETHINGY)
BEGIN..END
ELSE
BEGIN..END
--The above works I then insert this below and these if statement become nested----
IF(@A!= @SA)

IF(@S!= @SS)

IF(@C!= @SC)

IF(@W!= @SW)
--Inserted if statement stop here
END
ELSE <-- final else

所以会这样处理

IF(@A!= @SA){           
IF(@S!= @SS){
IF(@C!= @SC) {
IF(@W!= @SW){}
}
}
}

我期待的是这个

IF(@A!= @SA){}          
IF(@S!= @SS){}
IF(@C!= @SC){}
IF(@W!= @SW){}

我也尝试过这个,它抛出 Incorrect syntax near "ELSE". Expecting "CONVERSATION"

IF(@A!= @SA)
BEGIN..END
IF(@S!= @SS)
BEGIN..END
IF(@C!= @SC)
BEGIN..END
IF(@W!= @SW)
BEGIN..END

请注意,来自 ELSE <--final else down 现在嵌套在 IF(@W!= @SW) 内即使它是外部 if 语句的一部分 ELSE IF(SOMETHNGZ)之前。

编辑

根据要求我的完整声明

ALTER Procedure [dbo].[SP_PLaces]  
@ID int,
..more params
AS
BEGIN
SET NOCOUNT ON
DECLARE @SomeId INT
..more varaible
SET @SomeId = user define function()
..more SETS
IF(@ID IS NULL)
BEGIN
BEGIN TRY
INSERT INTO Places
VAlUES(..Values...)
... more stuff...
BEGIN TRY
exec Store procedure
@FIELD = 15, ... more params...
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS 'Message'
RETURN -1
END CATCH
RETURN 0
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS 'Message'
RETURN -1
END CATCH
END
ELSE IF(@ID IS NOT NULL AND @ID in (SELECT ID FROM Places))
BEGIN
SELECT @MyName = Name ...
...Some stuff....
IF(SOMETHNG_1)
BEGIN TRY
UPDATE ....
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS 'Message'
RETURN -1
END CATCH
ELSE IF(SOMETHNG_2)
BEGIN TRY
UPDATE ...
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS 'Message'
RETURN -1
END CATCH
ELSE
BEGIN
BEGIN TRY
UPDATE ...
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS 'Message'
RETURN -1
END CATCH
END
--The above works I then insert this below and these if statement become nested----
IF(@A!= @SA)
BEGIN
exec Stored procedure
@FIELD = 15,
... more params...
END
IF(@S!= @SS)
BEGIN
exec Stored procedure
@FIELD = 10,
... more params...
END
IF(@C!= @SC)
BEGIN
exec Stored procedure
@FIELD = 17,
... more params...
END
IF(@W!= @SW)
BEGIN
exec Stored procedure
@FIELD = 12,
... more params...
END
--Inserted if statement stop here
END
ELSE
BEGIN
SET @ResultMessage = 'Update/Delete Failed. No record found with ID:'+CONVERT(varchar(50), @ID)
SELECT @ResultMessage AS 'Message'
RETURN -1
END
Set NOCOUNT OFF
END

最佳答案

如果您要针对多个条件检查一个变量,那么您将使用类似的内容这里条件为真的代码块将被执行,其他 block 将被忽略。

IF(@Var1 Condition1)
BEGIN
/*Your Code Goes here*/
END

ELSE IF(@Var1 Condition2)
BEGIN
/*Your Code Goes here*/
END

ELSE --<--- Default Task if none of the above is true
BEGIN
/*Your Code Goes here*/
END

如果您要针对多个变量检查​​条件,则必须使用多个 IF语句,每个代码块将独立于其他 block 执行。

IF(@Var1 Condition1)
BEGIN
/*Your Code Goes here*/
END


IF(@Var2 Condition1)
BEGIN
/*Your Code Goes here*/
END


IF(@Var3 Condition1)
BEGIN
/*Your Code Goes here*/
END

在每个 IF 语句之后,如果执行了多个语句,则必须将它们放入 开始..结束 block 。无论如何,使用 BEGIN..END block 始终是最佳实践

更新

在您的代码中发现一些您缺少的 BEGIN END

ELSE IF(@ID IS NOT NULL AND @ID in (SELECT ID FROM Places))   -- Outer Most Block ELSE IF
BEGIN
SELECT @MyName = Name ...
...Some stuff....
IF(SOMETHNG_1) -- IF
--BEGIN
BEGIN TRY
UPDATE ....
END TRY

BEGIN CATCH
SELECT ERROR_MESSAGE() AS 'Message'
RETURN -1
END CATCH
-- END
ELSE IF(SOMETHNG_2) -- ELSE IF
-- BEGIN
BEGIN TRY
UPDATE ...
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS 'Message'
RETURN -1
END CATCH
-- END
ELSE -- ELSE
BEGIN
BEGIN TRY
UPDATE ...
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS 'Message'
RETURN -1
END CATCH
END
--The above works I then insert this below and these if statement become nested----
IF(@A!= @SA)
BEGIN
exec Store procedure
@FIELD = 15,
... more params...
END
IF(@S!= @SS)
BEGIN
exec Store procedure
@FIELD = 10,
... more params...

关于sql - SQL Server 中的多个单独的 IF 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20413064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com