oracle建表加导入数据,oracle批量建表并插入数据

在工作中需要测试语音详单数据,每次测试都要手工建表,插入数据来造数据。现在写一个脚本来处理这个问题,可以根据已有表来建立新的分表,并插入数据,并更新成自己想要的数据。

declare

tablename varchar2(40):='DR_GSM_931_';

date1 varchar(8):='20111101';   --语音详单开始日期

date2 varchar(8):='20111130';   --语音详单结束日期

vbill_month varchar(8);

tablename2 varchar2(40);

sql_stmt VARCHAR2(400);

vuser_number varchar2(11):='13948563233'; ---替换号码

vstart_time  varchar2(40):=sysdate-50;

FUNCTION isexisttable(t IN varchar) return varchar2 as

bt varchar2(10);

num number:=0;

begin

select count(*) into num from all_tables where table_name=t;

if num =0 then

bt:='false';

return bt;

else

bt:='true';

return bt;

end if;

end isexisttable;

PROCEDURE createtable(t IN varchar) as

begin

execute immediate

'create table '||t||'  as select * from DR_GSM_931_20111205 where 1=2';

end createtable;

PROCEDURE insertdata(t IN varchar) as

begin

execute immediate

'insert  into '||t||' select * from  DR_GSM_931_20111205 where rownum<50';

commit;

end insertdata;

PROCEDURE updatedata(t IN varchar) as

begin

sql_stmt := 'Update '||t||' Set bill_month=:vbill_month,user_number=:vuser_number,start_time=:vstart_time';

EXECUTE IMMEDIATE sql_stmt USING  vbill_month,vuser_number, vstart_time;

commit;

end updatedata;

PROCEDURE xddaydata(t IN varchar) as

begin

--DBMS_OUTPUT.PUT_LINE(dodata.isexisttable(t));

if isexisttable(t)='false' or isexisttable(t)='FALSE'

then

DBMS_OUTPUT.PUT_LINE(t||'表不存在,开始创建表');

createtable(t);

DBMS_OUTPUT.PUT_LINE(t||'表创建结束');

else

DBMS_OUTPUT.PUT_LINE(t||'表已存在,无需创建');

end if;

DBMS_OUTPUT.PUT_LINE('开始插入数据'||t);

insertdata(t);

DBMS_OUTPUT.PUT_LINE('数据插入完成');

updatedata(t);

DBMS_OUTPUT.PUT_LINE('数据更新完毕');

commit;

end xddaydata;

begin

for vbill_month in date1..date2 loop

tablename2:=tablename||vbill_month;

DBMS_OUTPUT.PUT_LINE('-------------------开始建表'||tablename2);

xddaydata(tablename2);

end loop;

DBMS_OUTPUT.PUT_LINE('end loop');

end;

你可能感兴趣的:(oracle建表加导入数据)