oracle存储过程,实现查询相同结构的表数据,按日期进行添加,若表中某日的数据已存在则将此条数据删除并添加以此数据id为键的新的数据。
基本结构:
create or replace procedure DATAANALYSIS_DAY_proc (dqbm in varchar2, strTime in varchar2) is
type cur is ref cursor; --定义游标
TABLE_CUR cur; -- 游标别称
tabel_name_count number; --定义number数据类型
isXExsite number; -- 定义number数据类型
BEGIN ----1
Open TABLE_CUR for -- 打开游标
'select count(table_name) from user_tables table_name like '''||UPPER(dqBM)||'HISTORY%' ';
FETCH TABLE_CUR INTO table_name_count; --将游标中的值赋给table_name_count变量。
CLOSE TABLE_CUR; --关闭游标
if table_name_count > 0 --判断表是否已存在
then
begin ----2
for i in 1..table_name_count -- 定义循环
LOOP
---使用游标获取数据
Open TABLE_CUR for -- 打开游标
'select count(*) from '||dqBM||'analysis'||i||' where id like '''||strTime||'%''';
FETCH TABLE_CUR INTO isExite; --将游标中的值赋给table_name_count变量。
CLOSE TABLE_CUR; --关闭游标
if isExite > 0
then --if后必须跟then
begin --- 3
--执行sql语句
execute immediate 'delete from '||dqBM||'analysis'||i||' where id like '''||strTime||'%''';
commit; --提交
DBMS_OUTPUT.put_line(strTime||'数据已删除!'); --输出测试语句
execute immediate 'insert into analysis'||i||' (select '||strTime||'2, field1,......,field900 from history1 where id like '''||strTime||'%'') ';
commit;--提交
end;---3
else
begin --- 4
execute immediate 'insert into analysis'||i||' (select '||strTime||'2, field1,......,field900 from history1 where id like '''||strTime||'%'') ';
end; ----4
end if;
END LOOP;
end; -----2
END; ----1
OK,一个简单的存储过程基本完成,敬请拍砖!