关于Oracle存储过程执行权限问题的解决
http://blog.sina.com.cn/s/blog_6ceed3280101hvlo.html
(2014-04-02 04:06:28)
分类: oracle-基础 |
定义存储过程时,通过指定AUTHID 属性,定义DR
Procedure 和IR Procedure
procedure DEMO(ID in NUMBER) AUTHID DEFINER as
...
...
procedure DEMO(ID in NUMBER) AUTHID CURRENT_USER as
...
...
用存储过程创建数据表:
创建时注意必须添加authid current_user ,如果创建的表已存在,存储过程继续执行,但如不不加此关键语句,存储过程将出现异常,
这个语句相当于赋权限。
As
v_sqlerrm VARCHAR2(4000) ;
v_temp_table varchar(30);
Begin
-- DELETE DUPLICATE DATA FROM table CAR.ZCXYXX_SZJSPX,KEEP THE LATEST ONE RECORD.
--CALL SP Proc_DISTINCT_ZCXYXX_SZJSPX('TEMP_ZCXYXX_SZJSPX')
v_temp_table:='TEMP_ZCXYXX_SZJSPX';
-- Call SP to delete duplicate records
Proc_DISTINCT_ZCXYXX_SZJSPX(v_temp_table);
MERGE INTO DRIVER_TRAIN_LOG Target USING
(
SELECT DISTINCT
DRIVER_NO ,--学员流水号
ID_NO ,--身份证明号码
TRAIN_CAR_CLASS --申请车型(准驾车型
FROM ZCXYXX_SZJSPX
) Source
ON
(
Source.DRIVER_NO = Target.DRIVER_NO -- AND
-- Source.TRAIN_CAR_CLASS = Target.TRAIN_CAR_CLASS
)
WHEN MATCHED THEN UPDATE SET
Target.ID_NO = Source.ID_NO ;
--Target.TRAIN_CAR_CLASS = Source.TRAIN_CAR_CLASS ;
COMMIT; --1
/* UPDATE TRAIN_CAR_CLASS */
MERGE INTO ZCXYXX_SZJSPX Target USING
(
SELECT DISTINCT
ID_NO ,--身份证明号码
TRAIN_CAR_CLASS --申请车型(准驾车型
FROM DRIVER_TRAIN_LOG
) Source
ON
(
Source.ID_NO = Target.ID_NO
)
WHEN MATCHED THEN UPDATE SET
Target.TRAIN_CAR_CLASS = Source.TRAIN_CAR_CLASS ;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
v_sqlerrm := sqlcode|| SQLERRM|| '存储过程[Proc_Update_ID_NO]执行失败!';
RAISE_APPLICATION_ERROR(-20003,v_sqlerrm);
ROLLBACK;
v_OUT := v_sqlerrm ;
End Proc_Update_ID_NO;
/*********************************
名称:Proc_DISTINCT_ZCXYXX_SZJSPX
功能描述:创建临时数据存储表
修订记录:
版本号 编辑时间 编辑人 修改描述
1.0.0 2015-9-20 ** 1.创建此存储过程
1.0.1 2015-9-21 ** 2.修改表名称及变量名称,增加必要注释
存储过程执行权限问题: AUTHID CURRENT_USER
入参出参描述:
IN_TABLE_NAME 要创建的临时表名
**********************************/
v_tablename varchar2(30);--表名
v_flag number(10,0);
v_sqlfalg varchar(200);
v_create_sql varchar(4000);
begin
v_flag:=0;
v_tablename:=UPPER(IN_TABLE_NAME);
v_sqlfalg:='select count(*) from user_TABLES where table_name='''||v_tablename||'''';
v_create_sql:='CREATE TABLE '|| v_tablename ||' AS SELECT * from
( select DRIVER_NO,ID_NO,NAME,TRAIN_CAR_CLASS,ACCEPT_DATE,REGIST_STATUS,OPERATION, rec_update_date,MEMO,
row_number() over (partition BY ID_NO order by rec_update_date desc) RN from ZCXYXX_SZJSPX WHERE RN=1)' ;
-- dbms_output.put_line(v_create_sql);
execute immediate v_sqlfalg into v_flag;
if v_flag=0 then --如果没有这个表 则去创建
begin
-- DELETE DUPLICATE DATA FROM table CAR.ZCXYXX_SZJSPX,KEEP THE LATEST ONE RECORD.
execute immediate v_create_sql;
execute immediate 'DROP TABLE ZCXYXX_SZJSPX';
execute immediate 'ALTER TABLE TEMP_ZCXYXX_SZJSPX rename TO ZCXYXX_SZJSPX';
end;
else
execute immediate 'DROP TABLE TEMP_ZCXYXX_SZJSPX';
-- DELETE DUPLICATE DATA FROM table CAR.ZCXYXX_SZJSPX,KEEP THE LATEST ONE RECORD.
execute immediate v_create_sql;
execute immediate 'DROP TABLE ZCXYXX_SZJSPX';
execute immediate 'ALTER TABLE TEMP_ZCXYXX_SZJSPX rename TO ZCXYXX_SZJSPX';
end if;
EXCEPTION
WHEN OTHERS THEN
--dbms_output.put_line( SQLCODE|| ' :'|| SQLERRM);
RAISE_APPLICATION_ERROR(-20003,SQLCODE|| SQLERRM|| '存储过程[Proc_DISTINCT_ZCXYXX_SZJSPX]执行失败');
ROLLBACK;
end;