Skip to Main Content
  • Questions
  • Need to generate script to delete values from various tables

Breadcrumb

XMAS_TREE

The AskTOM team is taking a break over the holiday season, so we're not taking questions or responding to comments. Please have a happy and safe festive season and as always, thanks for being a member of the AskTOM community.

Question and Answer

Connor McDonald

Thanks for the question.

Asked: September 15, 2016 - 4:28 am UTC

Last updated: September 17, 2016 - 3:49 am UTC

Version: oracle

Viewed 1000+ times

You Asked

I want to delete values from various tables. These table name are in a column field. Will this be possible .

For example lets say Table A has column as SID. And these SID are the table names too. I want to pick values from SID and delete values from these tables. Lets say Table A has value 123. Then my delete statement that need to be generated is Delete from A123.

and Connor said...

Some plsql should do the job

SQL> create table t ( tname varchar2(30) );

Table created.

SQL>
SQL> insert into t values ('TAB1');

1 row created.

SQL> insert into t values ('TAB2');

1 row created.

SQL>
SQL> set serverout on
SQL> declare
  2    del_stmt varchar2(1000);
  3  begin
  4   for i in ( select tname from t ) loop
  5     del_stmt := 'delete from '||i.tname;
  6     dbms_output.put_line(del_stmt);
  7     execute immediate del_stmt;
  8   end loop;
  9  end;
 10  /
delete from TAB1
delete from TAB2

PL/SQL procedure successfully completed.

SQL>
SQL>




We're not taking comments currently, so please try again later if you want to add a comment.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library