IF / ELSE in SQL (Control Structures) [closed]

-2

Okay, I'm reading some articles on SQL control structures, but I'm not getting a lot out of it, could anyone explain how it works in a summarized way?

    
asked by anonymous 07.06.2018 / 01:40

2 answers

1

I usually use the When clause instead of IF, however ....

Works similar to the programming structures of other languages.

In SQL Server, the IF ... ELSE statement is used to execute the code when a condition is TRUE or to execute a different code if the condition evaluates to FALSE.

Nothing better than a few lines of code to see how it works

DECLARE @teste INT;
SET @teste = 15;

IF @teste < 20
   PRINT 'Acertou';
ELSE
   PRINT 'ERRRROU';
GO

RETURN: Hit

As you can see he brought it right because he entered the first true condition that the test is worth 15 and less (

07.06.2018 / 03:55
0

The SQL language itself does not have an IF / ELSE command. You will find such a command in procedural languages associated with your database manager. In SQL you can use the conditional CASE / WHEN:

CASE WHEN condition THEN result
     [WHEN ...]
     [ELSE result]
END

In some cases the COALESCE and NULLIF functions may suit your needs.

    
07.06.2018 / 15:47