SQL Error (1093): You can’t specify target table 'import2016' for update in FROM clause

Asked

Viewed 802 times

0

I am running this PROCEDURE, and it returns this error!

BEGIN
        SET @cont = 0;
        REPEAT
            SET @sqlstring = "UPDATE import2016 SET coluna4 = (SELECT coluna4 FROM import2016 AS a WHERE a.coluna2 ='' and a.coluna4 like'%/%' LIMIT ?,01) WHERE id = (SELECT id+7 FROM import2016 AS a WHERE a.coluna2 ='' and a.coluna4 like'%/%' LIMIT ?,01);";
            PREPARE stmt FROM @sqlstring ;
            EXECUTE stmt USING @cont,@cont;
            DEALLOCATE PREPARE stmt;
            SET @cont = @cont +1;
        UNTIL @cont = 256
        END REPEAT;
    END


/* Erro SQL (1093): You can't specify target table 'import2016' for update in FROM clause */

PROCEDURE is on the same bench! I’d like to know how I can fix this mistake, thank you.

  • tried to use the aliases you put in the Queries ? UPDATE import2016 SET coluna4 = (SELECT a.coluna4 FROM import2016 AS a WHERE a.coluna2 ='' and a.coluna4 like'%/%' LIMIT ?,01) WHERE id = (SELECT a.id+7 FROM import2016 AS a WHERE a.coluna2 ='' and a.coluna4 like'%/%' LIMIT ?,01);

  • I’m sorry but I didn’t mean what you meant, I thank you for your reply!

  • in the subquerie, you put the table alias import2016 as a [import2016 AS a], but the select column was just coluna4, without the alias. I only suggested that you also put the alias in the sub selects, because SGDB can understand that that column is the table that is informed in the update. (Even if it is the same table)

  • 1

    In this way? "UPDATE import2016 AS a SET a.coluna4 = (SELECT a.coluna4 FROM import2016 AS a WHERE a.coluna2 ='' and a.coluna4 like'%/%' LIMIT ?,01) WHERE id = (SELECT a.id+7 FROM import2016 AS a WHERE a.coluna2 ='' and a.coluna4 like'%/%' LIMIT ?,01);";

1 answer

0


I managed to solve using the concept that @Rovann Linhalis said:

in subquerie, you put the alias of the import2016 table as the [import2016 as a], but the select column was only column4, without the alias. I merely suggested that you also add the alias to the sub selects, because SGDB can understand that that column is the table that is informed in update. (Even if it is the same table)

and the answer to another question here at stackoverflow:

Update with Select

As soon as the query was :

UPDATE capaobonito_import.import2016_copy 
SET coluna4 = 
(   
    SELECT b.coluna4 FROM import2016 AS b 
    WHERE b.coluna2 ='' and b.coluna4 like'%/%' LIMIT 00,01
) 
WHERE id =
(
    SELECT a.id+7 FROM import2016_copy_copy AS a 
    WHERE a.coluna2 ='' and a.coluna4 like'%/%' LIMIT 00,01
);

Grateful for the help of the Community, specifically the authors of comments and responses referenced here.

Browser other questions tagged

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