2013-01-15 170 views
0

成功以下代碼的程序使用SQL * Plus創建嵌套表

CREATE OR REPLACE PROCEDURE input_order (pat_id in char, vis_vdate in date, vis_act in number, 
vac_vacc in char) 
AS 
BEGIN 
DBMS_OUTPUT.PUT_LINE ('Insert attempted'); 
insert into vaccinations(pid,vdate,action,vaccinated) values(pat_id,vis_vdate,vis_act,vac_vacc); 
DBMS_OUTPUT.PUT_LINE ('Insert succeeded'); 
EXCEPTION 
WHEN others THEN DBMS_OUTPUT.PUT_LINE ('error'); 
DBMS_OUTPUT.PUT_LINE ('Insert rejected'); 
END; 
/

但是我的內涵是創建一個類似的過程,它包括填充嵌套表作爲表的屬性創建過程

  • 例如:假設 'vis_act' 是嵌套表
  • 類型vis_set_t
  • 和屬性visname和visurname

我嘗試過這種方式,但一直得到錯誤

CREATE OR REPLACE PROCEDURE input_order (pat_id in char, vis_vdate in date, vis_act in 
vis_set_t, vac_vacc in char) 
AS 
BEGIN 
DBMS_OUTPUT.PUT_LINE ('Insert attempted'); 
insert into vaccinations(pid,vdate,vis_set_t(visname,visurname),vaccinated) values 
(pat_id,vis_vdate,vis_act,vac_vacc); 
DBMS_OUTPUT.PUT_LINE ('Insert succeeded'); 
EXCEPTION 
WHEN others THEN DBMS_OUTPUT.PUT_LINE ('error'); 
DBMS_OUTPUT.PUT_LINE ('Insert rejected'); 
END; 
/
+0

這裏很好的例子:http://stackoverflow.com/questions/831188/how-to-創建一個存儲過程在oracle中它接受參數陣列 – Art

回答

0

一般插入例子。 正如我在評論中提到的,我不確定你想在你的程序中完成什麼。這是一般插入示例。隨意問更多的問題...:

DECLARE 
    TYPE EmpTabTyp IS TABLE OF scott.emp%ROWTYPE; 
    emp_tab EmpTabTyp:= EmpTabTyp(); 
-- 
PROCEDURE insert_emp(p_col_typ IN EmpTabTyp) 
IS 
BEGIN 
    SELECT * BULK COLLECT INTO emp_tab FROM scott.emp; 
-- 
    FOR i IN p_col_typ.first .. p_col_typ.last 
    LOOP 
    -- Insert your values into your table -- 
    INSERT Into emp_test (empno, ename) VALUES (p_col_typ(i).empno, p_col_typ(i).ename); 

    -- Optionally display values inserted - do not do this if table has many rows -- 
    --DBMS_OUTPUT.put_line('INSERTED :'|| p_col_typ(i).empno||chr(9)||p_col_typ(i).ename); 
    END LOOP; 

    DBMS_OUTPUT.put_line('Total rows inserted :'||p_col_typ.Count); 
END insert_emp; 
BEGIN 
    insert_emp(emp_tab); 
END; 
/

除了上面我的評論:

DECLARE 
    TYPE EmpTabTyp IS TABLE OF scott.emp%ROWTYPE; 
    emp_tab EmpTabTyp:= EmpTabTyp(); 
PROCEDURE display_emp (p_col_typ IN EmpTabTyp) 
IS 
BEGIN 
    SELECT * BULK COLLECT INTO emp_tab FROM scott.emp; 
-- 
    FOR i IN p_col_typ.first .. p_col_typ.last LOOP 
    DBMS_OUTPUT.put_line(p_col_typ(i).empno||chr(9)||p_col_typ(i).ename); 
    END LOOP; 
END display_emp; 
BEGIN 
    display_emp(emp_tab); 
END; 
/

DECLARE 
    l_names DBMS_UTILITY.maxname_array; -- or DBMS_UTILITY.name_array 
PROCEDURE show_contents (names_in IN DBMS_UTILITY.maxname_array) 
IS 
BEGIN 
    FOR indx IN names_in.FIRST .. names_in.LAST 
    LOOP 
    DBMS_OUTPUT.put_line (names_in (indx)); 
    END LOOP; 
END; 
BEGIN 
    l_names (1) := 'Picasso'; 
    l_names (2) := 'Keefe'; 
    l_names (3) := 'Dali'; 
    show_contents (l_names); 
END; 
/

-- returning collection type example: 
DECLARE 
    TYPE t_emptbl IS TABLE OF scott.emp%rowtype; 
    v_emptbl t_emptbl; 
    ret_val t_emptbl; 
Function getEmployeeList Return t_emptbl 
IS 
BEGIN 
    SELECT * bulk collect INTO v_emptbl FROM scott.emp; 
    -- Print nested table of records: 
    FOR i IN 1 .. v_emptbl.COUNT LOOP 
    DBMS_OUTPUT.PUT_LINE (v_emptbl(i).empno); 
    END LOOP; 
RETURN v_emptbl; 
END; 
BEGIN 
    ret_val:= getEmployeeList; 
END; 
/
+0

@藝術,我不明白你做的方式,但我已經更新了我的問題,我用哪個方法沒有工作 – lee

+0

@李,我的例子是一般的例子。我不知道你在通過在你的過程中使用嵌套表來完成什麼。要在某個表中插入值,你並不需要在你的過程中嵌套表。這將是更容易理解你添加創建表/插入腳本等...我會嘗試找到與嵌套表插入示例給你。但它將是基於scott.emp表的一般示例... – Art