gpt4 book ai didi

database - ORA-28113 : policy predicate has error

转载 作者:搜寻专家 更新时间:2023-10-30 23:42:05 28 4
gpt4 key购买 nike

我需要一些有关 Oracle 的 VPD 功能的帮助。我以前从未使用过它,但在网上做了一些研究,但是我遇到了一个问题。

以下是我采取的步骤:

QuanTriDL:

create table NhanVien2

table NhanVien2

QuanTriVPD:

CREATE OR REPLACE CONTEXT ThongTinTaiKhoan USING TTTK_PKG;

CREATE OR REPLACE PACKAGE TTTK_PKG IS
PROCEDURE GetTTTK;
END;
/

CREATE OR REPLACE PACKAGE BODY TTTK_PKG IS
PROCEDURE GetTTTK AS
TaiKhoan varchar(30);
tenPhong varchar(30);
tenChucVu varchar(30);
tenMaNV varchar(10);
BEGIN
TaiKhoan := LOWER(SYS_CONTEXT('USERENV','SESSION_USER'));

DBMS_SESSION.set_context('ThongTinTaiKhoan','GetTaiKhoan',TaiKhoan);
if (TaiKhoan = 'nv001') then
DBMS_SESSION.set_context('ThongTinTaiKhoan','GetChucVu','Giam doc');
else
if (TaiKhoan = 'nv002') then
DBMS_SESSION.set_context('ThongTinTaiKhoan','GetChucVu','Truong phong');
DBMS_SESSION.set_context('ThongTinTaiKhoan','GetPhong','Kinh doanh');
else
if (TaiKhoan = 'nv006') then
DBMS_SESSION.set_context('ThongTinTaiKhoan','GetChucVu','Truong phong');
DBMS_SESSION.set_context('ThongTinTaiKhoan','GetPhong','Ky thuat');
else
DBMS_SESSION.set_context('ThongTinTaiKhoan','GetChucVu','Nhan vien');
end if;
end if;
end if;
EXCEPTION
WHEN NO_DATA_FOUND THEN NULL;
END GetTTTK;
END;
/

CREATE OR REPLACE TRIGGER RangBuocTTTK AFTER LOGON ON DATABASE
BEGIN QuanTriVPD.TTTK_PKG.GetTTTK;
EXCEPTION WHEN NO_DATA_FOUND
THEN NULL;
END;
/

然后:

CREATE OR REPLACE FUNCTION Select_Nhanvien(
schema_p IN VARCHAR2,
table_p IN VARCHAR2)
RETURN VARCHAR2
AS
getChucVu varchar(50);
trave varchar2(1000);
BEGIN
SELECT SYS_CONTEXT('ThongTinTaiKhoan','GetChucVu') into getChucVu FROM DUAL;
trave := '1=2';
if (getChucVu = 'Giam doc') then
trave := NULL;
else
if (getChucVu = 'Truong phong') then
trave :='Phong=(SELECT SYS_CONTEXT(''ThongTinTaiKhoan'',''GetPhong'') FROM DUAL)';
else
trave :='TenTaiKhoan=(SELECT SYS_CONTEXT(''ThongTinTaiKhoan'',''GetTaiKhoan'') FROM DUAL)';
end if;
end if;
RETURN trave;
END;
/

BEGIN
DBMS_RLS.ADD_POLICY (
object_schema => 'QuanTriDL',
object_name => 'NhanVien2',
policy_name => 'VPD_Select_Nhanvien',
function_schema => 'QuanTriVPD',
policy_function => 'Select_Nhanvien',
statement_types => 'SELECT');
END;
/

当连接为 nv001、nv002、nv006 时就可以了。但是连接另一个用户:

ORA-28113: policy predicate has error

为什么会导致这个错误?

最佳答案

(多年的问题,但由于我偶然发现了它,所以我会继续为其他人回答...)

ORA-28113 只是意味着当您的策略函数返回一个 where 子句时,生成的 SQL 有一些错误。您可以通过查看跟踪文件来获取详细信息。另外,尝试:

select Select_Nhanvien('myschema','mytable') from dual;

然后将结果附加到 WHERE 子句中,如下所示:

SELECT * FROM MYTABLE WHERE <results from above>;

然后您应该看到根本原因。我猜想在上面的案例中,“其他用户”既没有构建 where 子句所需的 sys_context 变量,也没有访问登录触发器的权限。

作为旁注,您在这里可能遇到的另一个问题是当您的策略函数引用它自己的表时的循环引用——理想情况下,我希望策略函数在策略函数中绕过自身,这样您就可以不存在,等等,但是它似乎不是那样工作的。

关于database - ORA-28113 : policy predicate has error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33740973/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com