T-SQL 如何结束 IF-ELSE IF-ELSE 块
当我使用正确的参数运行下面的过程以便不返回 -1 值时,我的 DML 语句都不会触发.我猜它把我所有的 DML 语句当作 ELSE
块的一部分.
When I run the below procedure with the correct parameters so that the -1 value isn't returned, none of my DML statements are firing. I'm guessing that it's treating all my DML statements as part of the ELSE
block.
SQL Server 2014
如何结束 IF-ELSE-ELSE-IF 块?
How do I end an IF-ELSE-ELSE-IF block?
ALTER PROCEDURE [GenerateNumber] (
@Code VARCHAR(2)
)
AS
BEGIN
DECLARE @stringConcat VARCHAR = 'X';
IF @Code = 'KP'
SET @stringConcat += 'Y';
ELSE IF @Code = 'RL'
SET @stringConcat += 'Z';
ElSE
-- Return error code and stop processing
SELECT -1;
RETURN;
BEGIN TRY
-- Various DML statements...
SELECT @successValue;
RETURN;
END TRY
BEGIN CATCH
SELECT -1;
RETURN;
END CATCH
END
推荐答案
好的,你必须在 Else
语句中使用 Begin
和 End
因为它包含多行代码.
Okay you have to use Begin
and End
in the Else
statement as it contains multiple lines of code.
IF @Code = 'KP'
SET @stringConcat += 'Y';
ELSE IF @Code = 'RL'
SET @stringConcat += 'Z';
ElSE
Begin
-- Return error code and stop processing
SELECT -1;
RETURN;
End
相关文章