Query pass null parameter, bringing all values

Asked

Viewed 27 times

1

Hello,

I have a query in which I need you to bring me or the value passed in the parameter or, if no value is given, bring all information that is not null:

Example

select cd_medicamento, nm_medicamento, via_aplicacao, cd_grupo_medicamento
from medicamentos
where cd_grupo_materiais = :cd_grupo_materiais
and via_aplicacao = :via_aplicacao

In that case, I need you to bring all the materials of a particular group of materials and with a certain application route, but if no information is inserted in the application route, I need you to return all materials from this group of materials that have a registered value. I tried it both ways below and it didn’t work:

select cd_medicamento, nm_medicamento, via_aplicacao, cd_grupo_medicamento
from medicamentos
where cd_grupo_materiais = :cd_grupo_materiais
and ((via_aplicacao = :via_aplicacao) or (via_aplicacao is not null))

select cd_medicamento, nm_medicamento, via_aplicacao, cd_grupo_medicamento
from medicamentos
where cd_grupo_materiais = :cd_grupo_materiais
and case when :via_aplicacao is null
then via_aplicacao is not null
else via_aplicacao = :via_aplicacao
end

The field via_application receives a scan2 In the first example, the query always returns all the table values, even passing specific parameter to the application route. In the second example nothing returns to me

  • usually do nvl(Trim(:param_campox),campox) = campox

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.