Is it possible to do an Update this way?

Asked

Viewed 75 times

1

I have two tables, the Table Impressoras is where is stored the data of the equipment, as model, patrimony, etc. And I have the table Impressora Local where the location of the printer is stored, the client where it is currently allocated.

My table Impressoras has a column codigoempresa where when the printer and registered is placed the initial company where it goes, and in this table Impressora Local is stored a history of where the printer passed, for this reason in this table Impressora Local the company (company code) is always updated, but in the table Impressoras nay.

I was able to solve this issue of keeping this data updated in the table Impressora also creating a Funtion in the database that whenever a record is changed or added to the table Impressora Local the data (company code) is automatically updated. This works normally.

But the first time, I will have to update this data on the arm, for this reason I tried to do an Update command as follows:

UPDATE public.impressoras
    SET codigoempresa=(select codigoempresa from public.impressoralocal)
    WHERE public.impressoras.codigo = (select codigoimpressora from public.impressoralocal);

But the following error is returned to me:

ERROR:  more than one row returned by a subquery used as an expression
********** Error **********

ERROR: more than one row returned by a subquery used as an expression
SQL state: 21000

My question is, is it possible to update this table like this Impressoras, or I will be required to update line by line

Observing: This bank is a legacy for this reason I’m having to adopt such a measure

Table Printers:

inserir a descrição da imagem aqui

Table Printer Loca:

inserir a descrição da imagem aqui

  • put the table structure please, and what query you run on "first time"

  • @Rovannlinhalis as so first time?

  • you who said, "But the first time, I will have to update this data on the arm," that would be in INSERT ? which code you run ?

  • Oh yes, it’s just that today’s table Impressoras is with the codigoempresa outdated I will have to do the first Update command in hand, today I am changing so: UPDATE public.impressoras
 SET codigoempresa = 20 where
 codigoimpressora in (4,5,6). But there are many printers so I would like to know if there is a more effective way

  • I understand, if you can put the structure of the tables that helps a lot

2 answers

2


Disregarding the question that the column would not be necessary codigoempresa in the printer register, since you already have all the history in the other table:

For the multi-line problem, you can use a LIMIT 1 or MAX([idHistorico])

For your update that will run the first time manually, you can do so: [waiting for table structure to confirm fields]

Update impressoras set 
    codigoempresa = (
                     Select 
                         x.codigoempresa 
                     from impressoralocal x 
                     where x.codigoimpressora = impressoras.codigo 
                     order by x.codigo desc limit 1);

This code will update all records in impressoras

  • 1

    Perfect, that’s what I needed, thanks for your help

  • for nothing, have =]

0

The Queries within your SET and WHERE return more than one value because you do not filter which one you are specifically looking for.

Understand:

UPDATE public.impressoras
    SET codigoempresa=(select codigoempresa from public.impressoralocal WHERE public.nomeimpressora = 'Minha impressora')
    WHERE public.impressoras.codigo = (select codigoimpressora from public.impressoralocal WHERE public.nomeimpressora = 'Minha impressora');
  • Could you give an example?

  • still will resume more than one record in the subquery, because printer saves a history of the printers, may have more than one record with the same 'printer name'

  • 1

    Thanks for the help, I tried the @Rovannlinhalis and it worked perfectly for my need

Browser other questions tagged

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