2016-06-28 41 views
0

這是代碼:觀基礎上的功能,正確的查詢視爲壞

CREATE TABLE emp_where (where_clause VARCHAR2(4000)); 
INSERT INTO emp_where (where_clause) 
VALUES ('first_name=''KING'' or department_id = 20'); 
commit; 

CREATE OR REPLACE TYPE t_emp_rec AS OBJECT (
      EMPLOYEE_ID NUMBER(4), 
      FIRST_NAME VARCHAR2(10), 
      JOB_ID  VARCHAR2(9), 
      MANAGER_ID  NUMBER(4), 
      HIRE_DATE DATE, 
      SALARY  NUMBER(7,2), 
      DEPARTMENT_ID NUMBER(2) 
); 
/
CREATE OR REPLACE TYPE t_emp_tab AS TABLE OF t_emp_rec; 
/

CREATE OR REPLACE FUNCTION emp_fn RETURN t_emp_tab 
PIPELINED IS 
    l_sql VARCHAR2(32767); 
    l_where VARCHAR2(4000); 
    TYPE l_cur_type IS REF CURSOR; 
    l_cur l_cur_type; 
    l_rec employees%ROWTYPE; 
BEGIN 
    SELECT where_clause INTO l_where FROM emp_where; 
    l_sql := 'SELECT * FROM employees WHERE ' || l_where; 
    OPEN l_cur FOR l_sql; 
    LOOP 
    FETCH l_cur 
    INTO l_rec; 
    EXIT WHEN l_cur%NOTFOUND; 
    PIPE ROW(t_emp_rec(EMPLOYEE_ID => l_rec.EMPLOYEE_ID 
         ,FIRST_NAME => l_rec.FIRST_NAME 
         ,JOB_ID  => l_rec.JOB_ID 
         ,MANAGER_ID  => l_rec.MANAGER_ID 
         ,hire_date => l_rec.hire_date 
         ,SALARY  => l_rec.SALARY 
         ,DEPARTMENT_ID => l_rec.DEPARTMENT_ID)); 
END LOOP; 

RETURN; 
EXCEPTION 
WHEN OTHERS THEN 
    raise_application_error(-20000, SQLERRM || chr(10) || l_sql); 
END; 
/

CREATE OR REPLACE VIEW emp_vw AS 


UPDATE emp_where SET where_clause = 'EMPLOYEE_ID BETWEEN 100 and 200'; 
COMMIT; 
SELECT * FROM emp_vw; 

當我執行SELECT * FROM emp_vw;本條款:EMPLOYEE_ID BETWEEN 100 and 200 甲骨文給我一個錯誤

ORA-20000:ORA-06502 :PL/SQL:數字或值錯誤:數字精度太大 SELECT * FROM僱員WHERE EMPLOYEE_ID BETWEEN 100和200

但是,當我執行查詢本身(SELECT * FROM employees WHERE EMPLOYEE_ID在100和200之間)沒有錯誤。另一個場景 - when子句爲'deparment_id = 20'時,執行視圖是正確的。但是當我將'='更改爲'>'(department_id> 20)時 - numeric or value error: number precision too large。 有人可以解釋我是怎麼發生的嗎?

回答

1

如果我運行:

DESCRIBE employees 

然後我得到的輸出:

Name   Null  Type   
-------------- -------- ------------ 
EMPLOYEE_ID NOT NULL NUMBER(6)  
FIRST_NAME    VARCHAR2(20) 
LAST_NAME  NOT NULL VARCHAR2(25) 
EMAIL   NOT NULL VARCHAR2(25) 
PHONE_NUMBER   VARCHAR2(20) 
HIRE_DATE  NOT NULL DATE   
JOB_ID   NOT NULL VARCHAR2(10) 
SALARY     NUMBER(8,2) 
COMMISSION_PCT   NUMBER(2,2) 
MANAGER_ID    NUMBER(6)  
DEPARTMENT_ID   NUMBER(4)  

如果你把它比作你的t_emp_rec對象,然後你會看到,大多數對象屬性是一個更小的尺寸比表格列。

更改的對象具有相同的大小,它應該工作:

CREATE OR REPLACE TYPE t_emp_rec AS OBJECT (
    EMPLOYEE_ID  NUMBER(6), 
    FIRST_NAME  VARCHAR2(20), 
    JOB_ID   VARCHAR2(10), 
    MANAGER_ID  NUMBER(6), 
    HIRE_DATE  DATE, 
    SALARY   NUMBER(8,2), 
    DEPARTMENT_ID NUMBER(4) 
); 
/
+0

它來到我的腦海第二是mayby它的東西有不同勢的大小。但是我沒有超越表中的任何精度,所以我放棄了這個想法。 非常感謝,我會一直記住它,總是檢查這個...數字。 –