0

I want to delete the output of my query:-

SELECT client_id 
FROM   dt.pref 
WHERE  record_create_date < (SELECT current_timestamo - 45 YEARS 
                             FROM   sysibm.sysdummy1) 
UNION 
SELECT client_id 
FROM   ctt.dom 
WHERE  record_create_date < (SELECT current_timestamo - 45 YEARS 
                             FROM   sysibm.sysdummy1) 

Please suggest me how to perform delete operation here. Like I want to delete whatever is coming as output. That I need to use joins in my stored procedure

Rio
  • 23
  • 4

1 Answers1

0

Multi-Table delete (deleting from more than one table in the same DELETE) is not standard in SQL, but some platforms supports it.

MySQL does support a multi-table DELETE but its usually used where there is JOIN on the tables. https://dev.mysql.com/doc/refman/8.0/en/delete.html

DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;

This deletes from t1 and t2

Whether there is a requirement for a join or not, I don't know.

So maybe your SQL would look something like this, but somehow this just doesn't feel right. You'll just have to try it.

DELETE  dt.pref, ctt.dom FROM dt.pref, ctt.dom
WHERE  dt.pref.record_create_date < (SELECT current_timestamo - 45 YEARS FROM sysibm.sysdummy1) 
AND   ctt.dom.record_create_date < (SELECT current_timestamo - 45 YEARS FROM sysibm.sysdummy1) 

Probably a better option is to run two separate DELETE statements. If you must have them treated as a single statement, then wrap them in a TRANSACTION.

James Stevens
  • 374
  • 2
  • 8