Non-repetition of SQL SERVER code

-2

I have the following code in sql server. I need to make it in the first if it exits and execute the same statement that is inside the else tag so as not to repeat the code it has in there. How do I do this?

DECLARE @AG_NUM varchar(15)
DECLARE @BCO_NUM VARCHAR(3)
DECLARE @AG_NUM1 varchar(15)
DECLARE @BCO_NUM1 VARCHAR(3)

SELECT TOP 1 @BCO_NUM1 = BcoNUm, @AG_NUM1 = AgNum FROM CONTA_FIN WHERE ContaFinCCorNum = '{AgNum}'

SELECT @AG_NUM = AgNum, @BCO_NUM = BcoNum FROM DUPLICATA where DupNum = '{Duplicata}'

IF (@BCO_NUM is null) OR (@AG_NUM is null)
BEGIN

    UPDATE DUPLICATA SET BcoNum = @BCO_NUM1, AgNUm = @AG_NUM1 WHERE DupNum = '{Duplicata}'

    UPDATE PARC_DOC_FIN SET BcoNum = @BCO_NUM1, AgNum = @AG_NUM1 WHERE ParcDocFinDupNum = '{Duplicata}'

END
ELSE
   -- RESTO DO CODIGO
END.
    
asked by anonymous 02.04.2018 / 14:45

1 answer

1

You can use label and goto, even if it does not make sense in this context it would look like this:

DECLARE @AG_NUM varchar(15)
DECLARE @BCO_NUM VARCHAR(3)
DECLARE @AG_NUM1 varchar(15)
DECLARE @BCO_NUM1 VARCHAR(3)

SELECT TOP 1 @BCO_NUM1 = BcoNUm, @AG_NUM1 = AgNum FROM CONTA_FIN WHERE ContaFinCCorNum = '{AgNum}'

SELECT @AG_NUM = AgNum, @BCO_NUM = BcoNum FROM DUPLICATA where DupNum = '{Duplicata}'

IF (@BCO_NUM is null) OR (@AG_NUM is null)
BEGIN

    UPDATE DUPLICATA SET BcoNum = @BCO_NUM1, AgNUm = @AG_NUM1 WHERE DupNum = '{Duplicata}'

    UPDATE PARC_DOC_FIN SET BcoNum = @BCO_NUM1, AgNum = @AG_NUM1 WHERE ParcDocFinDupNum = '{Duplicata}'

    goto label1 /* a execução continuará logo após o "label1" ... */
END
ELSE
   -- RESTO DO CODIGO
   label1:
   /* exatamente aqui. */
END.
    
17.04.2018 / 22:47