title: ORA-28113: policy predicate has error
body: <p>I need some help with Oracle's VPD feature. I have never used it before but did some research online about it, however I'm running into a problem.</p>
  
  <p>Here are the steps that I have taken:</p>
  
  <p>QuanTriDL:</p>
  
  <pre><code>create table NhanVien2
  </code></pre>
  
  <p><a href="http://i.stack.imgur.com/8gTZz.jpg" rel="nofollow">table NhanVien2</a></p>
  
  <p>QuanTriVPD:</p>
  
  <pre><code>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;
  /
  </code></pre>
  
  <p>then:</p>
  
  <pre><code>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    =&gt; 'QuanTriDL',            
      object_name      =&gt; 'NhanVien2',            
      policy_name      =&gt; 'VPD_Select_Nhanvien',      
      function_schema  =&gt; 'QuanTriVPD',           
      policy_function  =&gt; 'Select_Nhanvien',      
      statement_types  =&gt; 'SELECT');
  END;
  /
  </code></pre>
  
  <p>When connecting as nv001, nv002, nv006 it's OK. But connecting another user:</p>
  
  <blockquote>
    <p>ORA-28113: policy predicate has error</p>
  </blockquote>
  
  <p>Why does it cause that error?</p>