0

I have a table abc and I am trying to update it but I keep getting the following error: You can't specify target table 'abc' for update in FROM clause.

Below is my SQL query:

UPDATE abc
SET value_1 = xxx
WHERE value_2 = 'ABC' AND id 
IN (Select id FROM abc WHERE value_3 = 'XYZ' AND value_1 = xxx);

Could anyone please tell me how I can make this query work? Many thanks.

Paulie-C
  • 1,674
  • 1
  • 13
  • 29
Tbas
  • 1
  • 1

3 Answers3

0

Whenever you use more than one table in an update statement - like you use subqueries or joins - you have to give the table, which has to be updated an alias (like i called the table myupd_table).
so you were just missing the FROM-part and the alias

UPDATE myupd_table
   SET value_1 = xxx
   FROM abc myupd_table
  WHERE myupd_table.value_2 = 'ABC' 
    AND myupd_table.id IN (Select id FROM abc WHERE value_3 = 'XYZ' AND value_1 = xxx);
Esteban P.
  • 2,789
  • 2
  • 27
  • 43
0

Following worked for me:

UPDATE abc
SET value_1 = xxx
WHERE value_2 = 'ABC' AND id 
IN (Select t1.id FROM (select * from abc) as t1 
WHERE t1.value_3 = 'XYZ' AND t1.value_1 = xxx);
Tbas
  • 1
  • 1
0

If id is a key (probably primary key) of your table, why not using this query instead:

UPDATE abc
   SET value_1 = xxx
 WHERE value_2 = 'ABC' 
   AND value_3 = 'XYZ'
   AND value_1 = xxx;

otherwise consider using better names for your fields.

Amir Pashazadeh
  • 7,170
  • 3
  • 39
  • 69