SQL Query Displays all logs

1

I have an application that should show in a dbgrid only the id that is equal to the service order and has the same id in another table:

The ID_OrdemServico comes from the ordem_servico table by foreign key to pecas_ordem_servico , I would like to show used parts only where IDs were equal

below the code that at the moment shows all the records:

select * from pecas_ordem_servico 
where pecas_ordem_servico.id_OrdemServico = Id_OrdemServico
    
asked by anonymous 26.11.2015 / 02:04

1 answer

1

The problem is that your Id_OrdemServico variable has the same column name in table pecas_ordem_servico

So the database understands that your SQL is like this:

select * from pecas_ordem_servico 
where pecas_ordem_servico.id_OrdemServico = pecas_ordem_servico.id_OrdemServico

Then it always returns everything, because the value of the id_OrdemServico column always equals the id_OrdemServico column. Got it?

Change the variable name to id_OrdemSer and do SQL like this:

select * from pecas_ordem_servico  where pecas_ordem_servico.id_OrdemServico = id_OrdemSer

It will work just the way you want it.

    
26.11.2015 / 03:00