IN This procedure you can search from any control i have 4 dropdown and two textbox for search you can set your parameter .In this procedure tarif,fabricant,nivel1,nivel2 is dropdown value and other two is textbox value.
in this procedure first we add our normal query in "@SqlQuery" when query assign in this variable.we add query on the basis of search criteria for EX.
IF (@Tarifa <> 'Select')
SET @SqlQuery= @SqlQuery + ' AND (Tarifa.Tarifa = ''' + @Tarifa +''')'
in this syntax @SqlQuery contain previos query and from this expression "AND (Tarifa.Tarifa = ''' + @Tarifa +''')'" we add condition and it will work fine
CREATE PROCEDURE [dbo].[SP_FilterPriceRule]
@Tarifa nvarchar(50),@Fabricante varchar(20), @Nivel1 varchar(50), @Nivel2 varchar(50),
@PorSobreCoste decimal(18,0) = NULL, @PorcentajeDescuento decimal(18,0) = NULL
AS
DECLARE @SqlQuery AS nvarchar(max)
DECLARE @SqlQuery1 AS nvarchar(max)
DECLARE @FinalQuery AS nvarchar(max)
print cast(@PorSobreCoste as varchar(10))
SET @SqlQuery='SELECT ReglaTarificacion.Id,convert(varchar,Tarifa.IdTarifa) +''-''+ Tarifa.Tarifa AS Tarifa, Fabricante.Descripcion AS Fabricante, Nivel1.Descripcion AS Nivel1, Nivel2.Descripcion AS Nivel2, ReglaTarificacion.PorSobreCoste AS PorSobreCoste,
ReglaTarificacion.PorcentajeDescuento AS PorcentajeDescuento, ReglaTarificacion.OrdenEjecucion
FROM ReglaTarificacion LEFT JOIN
Tarifa ON ReglaTarificacion.Idtarifa = Tarifa.IdTarifa LEFT JOIN
Fabricante ON ReglaTarificacion.CodFab = Fabricante.Codfab LEFT JOIN
Nivel2 ON ReglaTarificacion.IdNivel2 = Nivel2.Codigo_nivel2 LEFT JOIN
Nivel1 ON Nivel2.Codigo_nivel1 = Nivel1.Codigo_nivel1
where (1=1)'
IF (@Tarifa <> 'Select') SET @SqlQuery= @SqlQuery + ' AND (Tarifa.Tarifa = ''' + @Tarifa +''')'
IF (@Fabricante <>'Select') SET @SqlQuery=@SqlQuery + ' AND (Fabricante.Descripcion = ''' + @Fabricante +''')'
IF (@Nivel1 <>'Select') SET @SqlQuery=@SqlQuery + ' AND (Nivel1.Descripcion = ''' + @Nivel1 +''')'
IF (@Nivel2 <>'Select') SET @SqlQuery=@SqlQuery + ' AND (Nivel2.Descripcion = ''' + @Nivel2 +''')'
IF (@PorSobreCoste IS NOT NULL )
begin
SET @SqlQuery=@SqlQuery + ' AND (ReglaTarificacion.PorSobreCoste = '+ cast( @PorSobreCoste as varchar(10)) +')'
end
IF (@PorcentajeDescuento IS NOT NULL)
BEGIN
SET @SqlQuery=@SqlQuery + ' AND (ReglaTarificacion.PorcentajeDescuento = '+ cast( @PorcentajeDescuento as varchar(10)) +')'
END
SET @SqlQuery= @SqlQuery + ' ORDER BY ReglaTarificacion.OrdenEjecucion'
print @SqlQuery
exec(@SqlQuery)
if this procedure usefull for you then please Vote