0

I'm trying to updade a table with data that returned from two sub-selects.

If I execute the selects only, they return the correct value. But when I put then in the subselect, I receive an error message.

SQL Error [1093] [HY000]: You can't specify target table 'ORCAMENTO_ITEM_PORTFOLIO' for update in FROM clause

My script is

UPDATE  retool.ORCAMENTO_ITEM_PORTFOLIO
    SET  Q1 = (
            SELECT sum(oe.REALIZADO * co.COTACAO) as total
            FROM ORCADO_EXECUTADO oe
            INNER JOIN ORCAMENTO_ITEM_PORTFOLIO oip
                ON oe.ID_ORCAMENTO_ITEM_PORTFOLIO  = oip.ID_ORCAMENTO_ITEM_PORTFOLIO
            INNER JOIN COTACAO_ORCAMENTO co 
                ON co.ID_COTACAO = oip.ID_COTACAO 
            WHERE oe.ID_ORCAMENTO_ITEM_PORTFOLIO = oip.ID_ORCAMENTO_ITEM_PORTFOLIO AND oe.QUARTER = 'Q1' 
            ),
        Q2 = (
            SELECT sum(oe.REALIZADO * co.COTACAO) as total2
            FROM ORCADO_EXECUTADO oe
            INNER JOIN ORCAMENTO_ITEM_PORTFOLIO oip
                ON oe.ID_ORCAMENTO_ITEM_PORTFOLIO  = oip.ID_ORCAMENTO_ITEM_PORTFOLIO
            INNER JOIN COTACAO_ORCAMENTO co 
                ON co.ID_COTACAO = oip.ID_COTACAO 
            WHERE oe.ID_ORCAMENTO_ITEM_PORTFOLIO = oip.ID_ORCAMENTO_ITEM_PORTFOLIO AND oe.QUARTER = 'Q2' 
            )
    where 
        retool.ORCAMENTO_ITEM_PORTFOLIO.ID_ORCAMENTO_ITEM_PORTFOLIO > 0;
  • Please see [You can't specify target table for update in FROM clause](https://stackoverflow.com/questions/4429319/you-cant-specify-target-table-for-update-in-from-clause) – griv Nov 03 '22 at 19:38

0 Answers0