Oracle

需要有關此策略功能的幫助:顯示編譯錯誤

  • April 13, 2016
Create or Replace FUNCTION Hello_Fun ( P_SCHEMA IN varchar2, P_OBJECT IN 

varchar2) Return varchar2 IS
V_Hello Varchar2(400);
BEGIN
If User = 'Admin' then
V_Hello := '';
Else
V_Hello := 'cus_id in(select cus_id from employee where comp_id = NVL(SYS_CONTEXT('Hello_Context','COMP_ID'),0)');
END IF;
RETURN  V_Hello;
END;
/

所以 V_Hello:=‘cus_id….. 有編譯錯誤。它是否需要引號分隔符或有什麼問題:

LINE/COL  ERROR  
9/102  PLS-00103: Encountered the symbol "Hello_Context" 
when expecting one of the following: 
* & = - + ; < / > at in is mod remainder 
not rem <an exponent (**)> <> or != or ~= 
>= <= <> and or lik e LIKE2_ LIKE4_ LIKEC_ between 
|| multiset member SUBMULTISET_    

你剛剛結束了附近的單引號

**'** cus_id in(select cus_id from employee where comp_id = NVL(SYS_CONTEXT( **'**

以便編譯器期望;結束該語句,但它已經找到Hello_Context','COMP_ID'),0)');.

如果要在另一個單引號內使用單引號,可以使用q運算符,如下所示。詳情:字面量

SQL> Create or Replace FUNCTION Hello_Fun ( P_SCHEMA IN varchar2, P_OBJECT IN 
varchar2) Return varchar2 IS
V_Hello Varchar2(400);
BEGIN
If User = 'Admin' then
V_Hello := '';
Else
V_Hello := 'cus_id in(select cus_id from employee where comp_id = NVL(SYS_CONTEXT(' || q'['Hello_Context']' || ',' || q'['COMP_ID']' || '),0))';
END IF;
RETURN  V_Hello;
END;
/  2    3    4    5    6    7    8    9   10   11   12  

Function created.

SQL> select hello_fun('ADMIN','CUSTOMER') from dual;

HELLO_FUN('ADMIN','CUSTOMER')
--------------------------------------------------------------------------------
cus_id in(select cus_id from employee where comp_id = NVL(SYS_CONTEXT('Hello_Con
text','COMP_ID'),0))

引用自:https://dba.stackexchange.com/questions/135119