2
need to mount a conditional query with stored Procedure, to make clear my problem I will expose here to sp with what I want to work, note the end where I use Ifs, this there the problem.
USE [MarcenariaDigital]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PRO
insira o código aqui`insira o código aqui`CEDURE [dbo].[ps_precificacao]
-- Add the parameters for the stored procedure here
@fornecedorId int,
@marcaId int,
@corId int,
@descricao varchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
select pc.preco_id,
pc.preco_valor,
pc.preco_data_atualizacao,
m.material_descricao as material,
m.material_id as materialId,
c.cor_descricao as cor,
ma.marca_descricao as marca,
m.material_medida as medida,
um.unidade_medida_descricao as unidade,
DATEDIFF(day, pc.preco_data_atualizacao, SYSDATETIME()) as dias
from dbo.tb_precificacao pc
inner join (Select t.preco_material_id, max(t.preco_id) as maior
from dbo.tb_precificacao t
group by t.preco_material_id) tpc
on tpc.maior = pc.preco_id
right join dbo.tb_material m
on m.material_id = pc.preco_material_id
left join dbo.tb_cor c
on m.material_cor_id = c.cor_id
left join dbo.tb_marca ma
on m.material_marca_id = ma.marca_id
left join dbo.tb_unidade_medida um
on m.material_unidade_medida_id = um.unidade_medida_id
where m.material_inativo = 0
and pc.preco_fornecedor_id = @fornecedorId
if @corId > 0
and (m.material_cor_id = @corId or @corId is null)
if @marcaId > 0
and (m.material_marca_id = @marcaId or @marcaId is null)
if @descricao is not null
and (m.material_descricao like '%@descricao%' or @descricao is null)
END
how you did it will fetch the line using the parameter I passed, but if you pass 0 I do not want to return only the lines with null value, but yes, all, understood ?
– Mayllon Baumer
I changed the answer. If corID or marcaID is 0 it will return all lines. You can check if this is what you want?
– bruno
Success, thank you.
– Mayllon Baumer