Posted by Aaron | Posted in SQL | Posted on 24-02-2009
A request has been made for me to cover how to create a SQL If / Else statement. In this example, I am declaring a variable (@myVar), and using a select statement to set it equal to the value of table.columnName. Once you have a value, you can use that value in your If / Else statement. This example is checking to see if @myVar is greater than 10. If so, then the first SQL select statement is executed. If @myVar is not greater than 10, then the second SQL statement is executed.
DECLARE @myVar AS INTEGER SELECT @myVar = table.columnName FROM table WHERE columnName2 = condition IF ( @myVar > 10 ) BEGIN SELECT * FROM table WHERE columnName = @myVar END ELSE BEGIN SELECT * FROM table WHERE columnName < @myVar END
Related posts:



