简体   繁体   English

使用For Loop的PL / SQL过程

[英]PL/SQL procedure with For Loop

I am rather new to PL/SQL and am trying to understand how to code for this issue. 我是PL / SQL的新手,正在尝试了解如何为该问题编写代码。

I need to create 130 identical Tables, in 130 different Schemas using 130 different Tablespaces. 我需要使用130个不同的表空间在130个不同的模式中创建130个相同的表。 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: 目前,我正在使用的语句是直接的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. 我不确定如何执行此操作,因为我需要将“ n”传递给每个执行。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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