当我使用正确的参数运行下面的过程以便不返回 -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
这篇关于T-SQL 如何结束 IF-ELSE IF-ELSE 块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
Sql server 表使用情况统计Sql server table usage statistics(Sql server 表使用情况统计)
t sql中的相对路径?Relative path in t sql?(t sql中的相对路径?)
在 WHERE 条件下获取 SQL 中的最后一条记录Getting the last record in SQL in WHERE condition(在 WHERE 条件下获取 SQL 中的最后一条记录)
在 SQL Server 中使用 FOR XML PATH 查询以获取分层数据Query to get XML output for hierarchical data using FOR XML PATH in SQL Server(在 SQL Server 中使用 FOR XML PATH 查询以获取分层数据的 XML
嵌入在 sum() 函数中的 T-SQL IF 语句T-SQL IF statement embedded in a sum() function(嵌入在 sum() 函数中的 T-SQL IF 语句)
表与临时表性能Table vs Temp Table Performance(表与临时表性能)