1

I have more than 40 tables in RATOR_MONITORING schema for which the table name is starting from 'TEMP_'. I want to delete data from all such tables at once in a single query instead of using delete statement for each and every table. I dont even want to generate statements. I can create anonymous block if required but dont know how to do that. I tried below query but its not working.

Delete from RATOR_MONITORING_CONFIGURATION.'%TEMP_';
5
  • 1
    You should generate DML and use dynamic sql in pl\sql block, just lurk EXECUTE IMMEDIATE statement Commented Oct 6, 2015 at 8:00
  • 1
    As i said i dont want to generate sql statement because i want to try this logic in different schemas and every time then i have to generate sql statements and use dynamic sql in pl\sql block. But can you please give an example of what you said ? Commented Oct 6, 2015 at 8:03
  • 1
    You have to generate DML, there is no way to do such task without it. Take a look here stackoverflow.com/questions/832160/… there is examples close to your task. Look at example with EXECUTE IMMEDIATE. Commented Oct 6, 2015 at 8:06
  • Just a tip for future reference: saying "I don't want to X" is not helpful in a technical discussion. "I believe that I can't/shouldn't do X because of Y" is much more helpful and will get a more useful response. Commented Oct 7, 2015 at 6:12
  • @JeffreyKemp ok i will take care about this thanks Commented Oct 7, 2015 at 8:42

1 Answer 1

1

If you want to delete all the rows, then better use TRUNCATE, it will reset the high watermark. But remember, truncate is a DDL statement, and thus there will be an implicit commit; With DELETE you can commit manually after validation.

Although, I would not do that in a production environment. If it is something you are doing in test environment to build test data, then you could (ab)use EXECUTE IMMEDIATE.

For example, execute the following anonymous block as RATOR_MONITORING user:

DECLARE
  v_sql VARCHAR2(100);
BEGIN
  FOR i IN
  (SELECT table_name FROM user_tables where table_name like 'TEMP%'
  )
  LOOP
    v_sql := 'TRUNCATE TABLE '||i.table_name;
    EXECUTE immediate v_sql;
  END LOOP;
END;
/

By the way, using a good text editor, it won't take more than a minute to build DELETE/TRUNCATE statements and do it in pure SQL.

Sign up to request clarification or add additional context in comments.

4 Comments

Hello Lalit i dont want to delete data from the tables in schema. I want to delete data from Rator_Monitoring_Configuration schema for which the table name is starting from 'TEMP_' and yes i am trying this query in test environment first and if it works then i will use in production enevironment.
@Rahul No problem, just add a filter to the query in the for loop. I have updated the code.
I dont understood lalit why we have to use user_tables in select query Instead of providing schema name ?
@Rahul user_tables will have all the table_names, from which you will filter those tables starting with TEMP%. Without mentioning the table_name explicitly, you cannot execute the delete statement?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.