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;
/
4

1 回答 1

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;
/
于 2013-01-15T20:47:35.070 回答