How to loop through different schemas and execute some sql on each?

ε祈祈猫儿з 提交于 2019-12-23 18:35:35

问题


I have a case where I have 70 oracle schemas and I have to execute the same script on each

which would be the best way to achieve this.

Is it possible with a CURSOR?

For now I'm doing it with

ALTER SESSION SET current_schema = SCHEMA_1;
====
ALTER SESSION SET current_schema = SCHEMA_2;
====
ALTER SESSION SET current_schema = SCHEMA_3;
====

And I replace the "====" with my script, I+m doing it with Notepad++ but I have to prepare the script manually and if the script is long I have to split it in multiple chunks without new lines and do a replace for each chunk

I would like to automate this a little bit more.


回答1:


I offer the following semi-automatic way, which does not automate your task, but cuts down on the search and replace.

If you are using SQL*Plus, you can execute a file with the following syntax:

@myscriptfile.sql

If you would want to do that once for each schema, you could generate the code by querying the dictionary:

select 'ALTER SESSION SET current_schema = ' || owner || ';
       @myscriptfile.sql'
  from dba_users
 where <your filter>;

Then you would just copy/paste the result of that query in sqlplus. It is probably possible to spool that to file and execute it.



来源:https://stackoverflow.com/questions/5297268/how-to-loop-through-different-schemas-and-execute-some-sql-on-each

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!