3
下面是當程序運行時通過給P_LOC賦值爲'DALLAS' 它給出的錯誤爲-904 ORA-00904:「DALLAS」:請使用無效標識符提供一種解決方案oracle plsql將一個變量連接到一個字符串
create or replace
PROCEDURE PR_EMP_LST1
(P_LOC IN VARCHAR2
, P_MIN_SAL IN NUMBER
, P_MAX_SAL IN NUMBER
, P_REF_CUR OUT SYS_REFCURSOR
) AS
TYPE TY_LST_REFCURSOR IS REF CURSOR ;
cur_emp_lst TY_LST_REFCURSOR;
v_inv_query VARCHAR2(2000);
V_USG_CL VARCHAR2(200);
BEGIN
IF P_LOC IS NULL AND p_min_sal IS NULL AND p_max_sal IS NULL THEN
v_inv_query :='SELECT ENAME
FROM EMP ';
ELSE
v_inv_query :='SELECT ENAME
FROM EMP WHERE ';
IF P_LOC IS NULL AND p_min_sal IS NOT NULL AND p_max_sal IS NOT NULL THEN
v_inv_query :=v_inv_query ||' SAL BETWEEN '
||p_min_sal
||' AND '
|| p_max_sal;
-- v_inv_query :=v_inv_query ||' SAL BETWEEN :1 AND :2 ';
dbms_output.put_line('2');
-- V_USG_CL:= ' USING '||p_min_sal||' , '|| p_max_sal;
ELSIF P_LOC IS NOT NULL AND p_min_sal IS NOT NULL AND p_max_sal IS NOT NULL THEN
dbms_output.put_line('1');
v_inv_query :=v_inv_query||' SAL BETWEEN '
||p_min_sal || ' AND '|| p_max_sal
|| ' AND DEPTNO IN (SELECT DEPTNO
FROM DEPT
WHERE LOC= '
|| p_loc
||')';
dbms_output.put_line('2');
ELSIF P_LOC IS NOT NULL AND p_min_sal IS NULL AND p_max_sal IS NOT NULL THEN
v_inv_query :=v_inv_query||' SAL <= '
||p_max_sal
||' AND DEPTNO IN (SELECT DEPTNO
FROM DEPT
WHERE LOC= '
|| p_loc
||')';
ELSIF P_LOC IS NOT NULL AND p_min_sal IS NOT NULL AND p_max_sal IS NULL THEN
v_inv_query :=v_inv_query||' SAL >= '
||p_min_sal
||' AND DEPTNO IN (SELECT DEPTNO
FROM DEPT
WHERE LOC= '
||p_loc
||')';
ELSIF P_LOC IS NULL AND p_min_sal IS NULL AND p_max_sal IS NOT NULL THEN
v_inv_query :=v_inv_query||' SAL <= '
||p_max_sal;
ELSIF P_LOC IS NULL AND p_min_sal IS NOT NULL AND p_max_sal IS NULL THEN
v_inv_query :=v_inv_query||' SAL >= '
|| p_min_sal;
ELSIF P_LOC IS NOT NULL AND p_min_sal IS NULL AND p_max_sal IS NULL THEN
v_inv_query :=v_inv_query||' DEPTNO IN (SELECT DEPTNO
FROM DEPT
WHERE LOC= '
||p_loc
||')';
END IF;
END IF;
dbms_output.put_line('3');
dbms_output.put_line(v_inv_query);
OPEN cur_emp_lst FOR v_inv_query ;
dbms_output.put_line('4');
P_REF_CUR:=cur_emp_lst;
END PR_EMP_LST1;
它不工作,這次它將p_loc作爲'p_loc' – Bhagwat 2011-03-16 10:31:51
p_loc是什麼內容?是DALLAS(p_loc ='DALLAS')還是'DALLAS'(p_loc ='''DALLAS''')。如果是DALLAS,則v_inv_query:= v_inv_query ||' SAL BETWEEN' || p_min_sal || 'AND'|| p_max_sal || 'AND DEPTNO IN(SELECT DEPTNO FROM DEPT where LOC =''' || p_loc ||''')';應該管用。字符串內''的意思是'。 – HamoriZ 2011-03-16 11:22:57
感謝您的工作精細可以嗎?請解釋爲什麼它應該這樣 – Bhagwat 2011-03-17 13:47:26