SQL Query Shows All Records

Asked

Viewed 223 times

1

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

To ID_OrdemServico comes from the table ordem_servico by foreign key to pecas_ordem_servico, would like to show the used parts only where the 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
  • Opa, I was confused with the names/ids of the tables, but try ai: select * from pecas_ordem_servico Where pecas_ordem_servico.Id_ordemservico = ordem_servico.Id_ordemservico, to no celular hahaha se funciona avisa q revemos!

1 answer

1


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

So the database is understanding that your SQL is like this:

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

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

Rename the variable to id_OrdemSer and makes SQL like this:

select * from pecas_ordem_servico  where pecas_ordem_servico.id_OrdemServico = id_OrdemSer

It’ll work out the way you want it.

  • negative, I changed the name to ID only and displays this message

  • Multi-part identifier "ordem_servico.ID" could not be associated.

  • You have q alter the variable not the column, vc changed the column and ta saying that the column ID not in table ordem_servico does not exist... And there really isn’t

  • select * from pecas_ordem_servico Where pecas_ordem_servico.id_OrdemServico = :Id_ordemservico

  • the X of the question was to leave a space after the end, I thank all.

  • In this sql of the comment you put an : in front of the variable, there yes it identifies as variable and not as the column, a space at the end of an SQL command does not change anything in any database, but that good q solved :)

Show 1 more comment

Browser other questions tagged

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