sql server 2014 - SQL - CASE statement to filter the query -
sql server 2014 - SQL - CASE statement to filter the query -
hi having stored procedure selecting same columns, query follows
create procedure get_data @in_filter_column1 nvarchar(6), @in_filter_column2 nvarchar(50), @in_flag bit = 0 begin if @in_flag = 0 select t1.column1, t1.colum2, t2.column3 table1 t1 inner bring together table2 t2 on t1.column1 = t2.column1 t1.column1 = @in_filter_column1 else select t1.column1, t1.colum2, t2.column3 table1 t1 inner bring together table2 t2 on t1.column1 = t2.column1 inner bring together table3 t3 on t3.column1 = t2.column1 t3.column1 = @in_filter_column2 end
so here instead of writing same select status using switch case or other alternative way. possible
one way know trying other
create procedure get_data @in_filter_column1 nvarchar(6), @in_filter_column2 nvarchar(50), @in_flag bit = 0 begin declare @sql_query nvarchar(max) set @sql_query = ' select t1.column1, t1.colum2, t2.column3 table1 t1 inner bring together table2 t2 on t1.column1 = t2.column1' if @in_flag = 0 set @sql_query = @sql_query + ' t1.column1 = ''' + @in_filter_column1 +'''' else set @sql_query = @sql_query + ' inner bring together table2 t3 on t3.column1 = t1.column1 t3.column1 = ''' + @in_filter_column2 +'''' end
sql-server-2014
Comments
Post a Comment