sql server - IF statement in SQL WHERE clause -
is possible pass in value stored procedure tell whether or not append or statement sql select statement?
i've tried not valid:
select xyzname xyz_fields (xyztype = 'this') union (if @status=1 or (xyztype = 'that')) union (if @status2=1 or (xyztype = 'somethingelse'))
kind of building clause in sql rather hitting db again application?
you can use dynamic sql this.
declare @sqlquery varchar(100) set @sqlquery = ' select xyzname xyz_fields xyztype = ''this'' ' if(@status=1) set @sqlquery = @sqlquery + ' or xyztype = ''that'' ' if(@status2=1) set @sqlquery = @sqlquery + ' or xyztype = ''somethingelse''' exec(@sqlquery)
single qoutes in query escaped prefixing single qoute. in query
where xyztype = 'this'
should be
where xyztype = ''this''
Comments
Post a Comment