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?
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?
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 (
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.