码迷,mamicode.com
首页 > 数据库 > 详细

oracle_存储过程小记

时间:2015-01-05 20:26:36      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:

# 刷新会员标签函数 {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}

oracle_存储过程小记

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!