Site icon Database Tutorials

IF…ELSE Statement in SQL Server(TSQL)

If the condition in the IF block is provided, operations between the BEGIN and END blocks are performed. If the condition in the IF block is not provided, the operations between the BEGIN and END blocks after the ELSE block are performed.

You can use it as follows. We’re running the same IF … ELSE Statement twice by changing the parameter value. Thus, we enter both the IF block and the ELSE block.

For a more detailed example, we create a table as below and add a few records.

Then, with the help of the following script, we find the maximum age in the table and if the maximum age is greater than 50, we delete that record or records from the table. If the maximum age is not greater than 50, we print a message.

Exit mobile version