sql server - How to write a sql query for different conditions in where clause on demand? -
i have pass different parameters columns code behind, written 15 gridview binding methods 15 columns, avoid set in query.for example if column 1 pick clause , if column 2 pick clause.so how write this?
here example of how create , run dynamic queries
create procedure sp_search @param1 int = null, @param2 int = null begin declare @query_string nvarchar(4000); set @query_string = 'select * table_name 1 = 1 '; if(@param1 not null) begin set @query_string = @query_string + ' , columnname1 = ' + cast(@param1 varchar); end if(@param2 not null) begin set @query_string = @query_string + ' , columnname2 = ' + cast(@param2 varchar); end exec sys.sp_executesql @query_string; end
Comments
Post a Comment