Oracle数据库 循环建表语句及多表循环插入语句

Oracle数据库的循环建表语句及多表循环插入语句

--建多张表的存储过程

create  or replace procedure  createtables

is

i number (10) :=1;

begin

for i in 1..10 loop

execute immediate  'create table zry'||i||'

(k1 NUMBER NOT NULL PRIMARY KEY,

k2 VARCHAR2(10),

k3 VARCHAR2(50),

k4 DATE,

k5 CHAR(2)

)';

end loop;

end createtables;


--调用存储过程
execute createtables


--删除存储过程
drop procedure createtables


--给多张表插入多条记录的sql语句

begin 

    declare i int; j int; tbname varchar2(50);

begin

for i in 1..5 loop

    tbname := 'zry'||to_char(i);

for j in 1..3 loop

execute immediate 'INSERT INTO '||tbname||'(k1,k2,k3,k4,k5) values('||j||',''zry'',''west'',to_date(''2017-08-31'',''yyyy-mm-dd''),''01'')';

end loop;

end loop;

commit;

end;

end;









你可能感兴趣的:(数据库)