I have a doubt about SQL. Sometimes is necessary to have some sort of ifs in queries, however, there are no ifs in SQL right? So it seems that we should handle these cases with logic operators but I'm not understanding properly how this works, how these logic operators can be used as ifs. Do you know how that works?
closed as unclear what you're asking by mustaccio, Philᵀᴹ, Vérace, marcello miorelli, Marco Apr 21 at 18:19
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Consider SQL Server's CASE statement.
If you're trying to say:
if x=3 then I want this, else I want that
...then you could say:
SELECT CASE x WHEN 3 THEN this ELSE that END
Microsoft SQL Server supports various imperative constructs, you can wrap statements around logical conditions using IF/ELSE. For instance, you can use the following lines to only execute code on a specific server:
IF @@SERVERNAME = 'ProductionServer' BEGIN PRINT 'ProductionServer' END ELSE BEGIN PRINT 'Please login to the Production Server.' END
You can also use logical functions such as CHOOSE or IIF (Shorthand for CASE).