sql server - Statement blocks in SQL SELECT using IF ELSE -
i'm trying return different data depending on variable in select
. this:
select if @variable = 'yes' column1, column2 else column3 table
what proper way use if
condition in sql? or there better alternative i'm trying accomplish?
you can use if statement, you'll need set multiple queries. can use case selecting 1 column or another, not select 1 or multiple in question.
declare @var int = 1; declare @test table ( col1 int, col2 int, col3 int ) insert @test values (1,2,3) if @var = 1 begin select col1, col2 @test end else begin select col3 @test end
Comments
Post a Comment