简体   繁体   中英

PL/SQL procedure with For Loop

I am rather new to PL/SQL and am trying to understand how to code for this issue.

I need to create 130 identical Tables, in 130 different Schemas using 130 different Tablespaces. I can readily run the code, then do global Search/Replace for the next schema, run the code, and repeat.

What I want to do is write an anonymous block with

declare n number(3);
  Begin
   for n in 1..130 
   Loop

    (run my statements)

  End Loop;
End;
/

Currently the statements I am using is a straight SQL:

CREATE TABLE xyz_101.... Tablespace xyz_101

I am thinking I should create variables to hold all the Create Table, Alter Table, Create Index, Create Synonym syntax, then execute immediate this variables. I am not completely certain how to do this as I will need pass "n" to each execution.

Is there a better way? Should I write the "Create Table", Create Index", "Create Synonym" statements as cursors and then execute the cursors?

I am certain someone else has solved this problem and appreciate any guidance or insight. Thank you!

Use EXECUTE IMMEDIATE.

 for n in 1 .. 130 loop
       execute immediate 'create table t'||n||' ( dummy char(1) )';
 end loop;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM