本文介紹了T-SQL 如何結(jié)束 IF-ELSE IF-ELSE 塊的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
當(dāng)我使用正確的參數(shù)運(yùn)行下面的過程以便不返回 -1 值時(shí),我的 DML 語句都不會觸發(fā).我猜它把我所有的 DML 語句當(dāng)作 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
如何結(jié)束 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
因?yàn)樗嘈写a.
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
這篇關(guān)于T-SQL 如何結(jié)束 IF-ELSE IF-ELSE 塊的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!