oracle_存储过程小记

# 刷新会员标签函数 {color:red} fun_refresh_code{color}
{noformat}
CREATE OR REPLACE FUNCTION fun_refresh_code(v_code NUMBER := 0)
RETURN varchar2 IS
vsql VARCHAR2(1000);
outpass VARCHAR2(100);
outfail VARCHAR2(100);
refreshresult VARCHAR2(100);
errormgs VARCHAR2(1000);
BEGIN
outpass := 'mview_css_mbrlabel_' || v_code || '刷新成功';
outfail := 'mview_css_mbrlabel_' || v_code || '刷新失败';
vsql := 'call dbms_mview.refresh(' || 'mview_css_mbrlabel_' || v_code || ')';
execute immediate vsql;
refreshresult := outpass;
INSERT INTO css_procExecLog
(UUID, CREATED, errorMsg, tablename, operation, STATUS)
VALUES
(sys_guid(),
sysdate,
'',
'mview_css_mbrlabel_' || v_code,
'refresh',
'success');
COMMIT;
-- return outpass;
Exception
When others then
refreshresult := outfail;
errormgs := sqlcode || sqlerrm(sqlcode);
INSERT INTO css_procExecLog
(UUID, CREATED, errorMsg, tablename, operation, STATUS)
VALUES
(sys_guid(),
sysdate,
errormgs,
'mview_css_mbrlabel_' || v_code,
'refresh',
'failed');
COMMIT;
-- return outfail;
return refreshresult;
END;
{noformat}
# 删除会员标签函数 {color:red} fun_delete_code{color}
{noformat}
CREATE OR REPLACE FUNCTION fun_delete_code(v_code NUMBER := 0) RETURN varchar2 IS
vsql VARCHAR2(1000);
outpass VARCHAR2(100);
outfail VARCHAR2(100);
deleteresult VARCHAR2(100);
errormgs VARCHAR2(1000);
BEGIN
outpass := 'mview_css_mbrlabel_' || v_code || '删除成功';
outfail := 'mview_css_mbrlabel_' || v_code || '删除失败';
vsql := '(drop MATERIALIZED VIEW ' || 'mview_css_mbrlabel_' || v_code || ')';
execute immediate vsql;
deleteresult := outpass;
INSERT INTO css_procExecLog
(UUID, CREATED, errorMsg, tablename, operation, STATUS)
VALUES
(sys_guid(),
sysdate,
'',
'mview_css_mbrlabel_' || v_code,
'delete',
'success');
COMMIT;
-- return outpass;
Exception
When others then
deleteresult := outfail;
errormgs := sqlcode || sqlerrm(sqlcode);
INSERT INTO css_procExecLog
(UUID, CREATED, errorMsg, tablename, operation, STATUS)
VALUES
(sys_guid(),
sysdate,
errormgs,
'mview_css_mbrlabel_' || v_code,
'delete',
'failed');
COMMIT;
-- return outfail;
return deleteresult;
END;
{noformat}
# 刷新视图 {color:red} mbrlabelrefresh{color}
{noformat}
CREATE OR REPLACE PROCEDURE mbrlabelRefresh IS
v_code css_label_mview.code%TYPE;
vsql_mview_css_mbr VARCHAR2(1000);
vsql_css_label_mview VARCHAR2(1000);
vsql_mbrlabelproc VARCHAR2(1000);
vsql_mbrlabelviewproc VARCHAR2(1000);
vsql_mbrlabellogproc VARCHAR2(1000);
vsql VARCHAR2(1000);
-- 定义变量
CURSOR code_cursor IS
select l.code
from css_label_mview l
where not exists (select *
from user_mviews vl
where l.code = SUBSTR(vl.mview_name, 20));
-- 定义游标
BEGIN
vsql_mview_css_mbr := call dbms_mview.refresh('mview_css_mbr');
vsql_css_label_mview := call dbms_mview.refresh('css_label_mview');
-- 刷新会员和标签表的语句
vsql_mbrlabellogproc := call vsql_mbrlabellogproc();
-- Css库生成日志
vsql_mbrlabelproc := call MBRLABELPROC();
-- 生成会员标签语句
vsql_mbrlabelviewproc := call MBRLABELVIEWPROC();
-- 刷新会员标签关系语句
execute immediate vsql_mbrlabellogproc;
-- 执行生成css库的日志表
execute immediate vsql_mview_css_mbr;
execute immediate vsql_css_label_mview;
-- 执行刷新会员和标签表的语句
execute immediate vsql_mbrlabelproc;
-- 执行生成会员标签语句
FOR code_cur IN code_cursor LOOP
begin
v_code := code_cur.code;
select fun_refresh_code(v_code) from dual;
end;
END LOOP;
-- 调用函数进行刷新
-- 刷新已有标签视图,按照游标查询出来的code,对每个code的物化视图刷新一次,并记录日志
execute immediate vsql_mbrlabelviewproc;
-- 执行存储过程,将会员跟标签关系刷新
END mbrlabelRefresh;
{noformat}
# 删除视图 {color:red} mviewmbrlabeldelete{color}
{noformat}
CREATE OR REPLACE PROCEDURE mviewmbrlabeldelete IS
v_code css_label_mview.code%TYPE;
vsql VARCHAR2(1000);
-- 定义变量
CURSOR code_cursor IS
select l.code
from css_label_mview l
where not exists (select *
from user_mviews vl
where l.code = SUBSTR(vl.mview_name, 20));
-- 定义游标
BEGIN
FOR code_cur IN code_cursor LOOP
begin
v_code := code_cur.code;
select fun_delete_code(v_code) from dual;
END LOOP;

-- 调用函数进行刷新 
-- 将游标取出的物化视图删除掉,删除不了记录日志
END mviewmbrlabelDelete;
{noformat}

原文地址:https://www.cnblogs.com/Sir-Li/p/4204375.html