一:无返回值的存储过程
存储过程:
create or replace procedure DATA_TEST_PROC (dqBM in varchar2,strTime in varchar2)
is
type cur is ref cursor ; --定义游标
TABLE_CUR cur; --设置游标别名
tabel_name_count number; --定义number类型变量
isExite number; --同上
i number :=1; -- 定义number类型变量并赋初始值
BEGIN -- 1
Open TABLE_CUR for
'select count(table_name) from user_tables where table_name like '''||UPPER(dqBM)||'HISTORY%''';
FETCH TABLE_CUR INTO tabel_name_count; --获取游标存储值
CLOSE TABLE_CUR; -- 关闭游标
if tabel_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;
CLOSE TABLE_CUR;
if isExite > 0
then
begin -- 3
execute immediate ' delete from '||dqBM||'analysis'||i||' where id like ''' ||strTime||'%'';
commit;
DBMS_OUTPUT.put_line(strTime||' 数据已删除');
end;-- 3
end loop;
end; -- 2
END;-- 1
create or replace procedure proc_getHisTabCount (dqbm in varchar2, hisTabCount OUT integer)
is
--声明全局变量
var_sql String(32765);
sql_select varchar2(1000);
num_count number;
type CurType is ref cursor;-- 定义游标引用
curTerm CurType; -- 设置游标别名
BEGIN
Open curTerm for
'select count(*) from user_all_tables where table_name like '''||UPPER(dqbm)||'HISTORY%''';
FETCH curTerm INTO num_count;
CLOSE curTerm;
if num_count > 0
then
hisTabCount := num_count;
end if;
END proc_getHisTabCount;
create or replace package pack_cursor is
-- Author : ADMINISTRATOR
-- Created : 2010-4-8 10:29:16
-- Purpose : 创建游标获取数据列表
-- Public type declarations 公共类型声明
type TYPE_CURS is REF CURSOR; --创建游标引用
end pack_cursor;
b. 建立存储过程,如:
create or replace procedure proc_getcursor_value(p_cursor out PACK_CURSOR.TYPE_CURS)
is
type type_cur is ref cursor;
term_type type_cur;
cur_value varchar2(20);
rows_num integer;
begin
Open term_type for
select name from measureclass t where display = '3';
loop
fetch term_type into cur_value; -- 从游标p_cursor中读取值
exit when term_type%notfound; --(dbms_sql.fetch_rows(p_cursor)>0) 通过dbms_sql.fetch_rows(p_cursor)获取游标的行
if term_type%found
then
dbms_output.put_line(cur_value);
end if;
end loop;
close term_type;
end proc_getcursor_value;