Column3 is not being referenced anywhere outside of the IF
and ELSE
blocks. If you wish to reference this value you will need to declare a new variable and use that;
DECLARE @btColumn3 BIT
SELECT @btColumn3 = Column3 FROM @tblTableA
IF @btColumn3 = 0
SELECT Column1, Column2 FROM
@tblTableA WHERE
Column2 > 200
ELSE
SELECT Column1, Column2 FROM
@tblTableA WHERE
Column2 < 200
Or do the following;
IF (SELECT Column3 FROM @tblTableA) = 0
SELECT Column1, Column2 FROM
@tblTableA WHERE
Column2 > 200
ELSE
SELECT Column1, Column2 FROM
@tblTableA WHERE
Column2 < 200
Either way you will have to ensure that the query used to retrieve Column3 returns a single result either by limiting your query so that it can only return a single value or using MIN()
, MAX()
etc depending on your requirements.
Also, if you need to execute more than one query within the IF
and ELSE
blocks you will need to wrap the contents in BEGIN
and END
as follows:
IF @btColumn3 = 0
BEGIN
// Do a query
// Do another
END
ELSE
BEGIN
// Do a query
// Do another
END