更新嵌套表中元素的属性

发布于 2024-08-17 14:04:44 字数 2214 浏览 5 评论 0原文

我创建了这种类型: 创建或替换类型 PRODTABLE 作为 PROD_OBJ 表;

我在下面的 PLSQL 代码中使用该 PRODTABLE:

    FUNCTION  INSERT_PRODUCTS (

      a_supplier_id IN FORNECEDOR.ID_FORNECEDOR%TYPE,

      a_prodArray IN PRODTABLE

      ) 

     RETURN NUMBER IS

        v_error_code    NUMBER;
        v_error_message VARCHAR2(255);
        v_result        NUMBER:= 0;
        v_prod_id       PRODUTO.ID_PROD%TYPE;
        v_supplier      FORNECEDOR%ROWTYPE;
        v_prodInserted  PROD_OBJ;
        type nestedTable is table of PROD_OBJ;
        newList   nestedTable := nestedTable();

     BEGIN

     SELECT  FORNEC_OBJ(ID_FORNECEDOR,NOME_FORNECEDOR,MORADA,ARMAZEM,EMAIL,TLF,TLM,FAX) into v_supplier from fornecedor where id_fornecedor = a_supplier_id;

      FOR i IN a_prodArray.FIRST .. a_prodArray.LAST LOOP

          INSERT INTO PRODUTO (PRODUTO.ID_PROD,PRODUTO.NOME_PROD,PRODUTO.PREC_COMPRA_PROD,PRODUTO.IVA_PROD,PRODUTO.PREC_VENDA_PROD,PRODUTO.QTD_STOCK_PROD,PRODUTO.QTD_STOCK_MIN_PROD) 
          VALUES (S_PRODUTO.nextval,a_prodArray(i).NOME_PROD,a_prodArray(i).PREC_COMPRA_PROD,a_prodArray(i).IVA_PROD,NULL,NULL,NULL);

          SELECT ID_PROD into v_prod_id from PRODUTO where NOME_PROD = a_prodArray(i).NOME_PROD;

          INSERT INTO PROD_FORNECIDO VALUES (a_supplier_id, v_prod_id,a_prodArray(i).PREC_COMPRA_PROD);

          SELECT PROD_OBJ(ID_PROD,NOME_PROD,PREC_COMPRA_PROD,PREC_VENDA_PROD,QTD_STOCK_PROD,QTD_STOCK_MIN_PROD,IVA_PROD) into v_prodInserted from PRODUTO where ID_PROD= v_prod_id;
          newList.extend;
          newList(newList.last):= v_prodinserted;

        END LOOP;

        /*the next line generates Error(43,63): PLS-00642: local collection types not      allowed in SQL statements,
Error(43,63): PL/SQL: ORA-00932: inconsistent datatypes: expected UDT got CHAR */
        INSERT INTO FORNECPRODS2 VALUES (a_supplier_id,v_supplier,newList);
        v_result:= 1; 
        RETURN v_result;
        COMMIT;
    (...)
    END;

我想做一些类似 a_prodArray(i):= v_prodInserted 的事情,但我不能,因为它是一个嵌套表,所以我需要检索另一个表用于插入或使用新插入的产品更新嵌套表的每个元素。我必须这样做,因为 PRODTABLE 中的每个 prod_obj 都带有来自 JAVA 的 id=0 。 也许还有另一种方法,比如使主键的默认值等于 SEQUENCE.nextval,我不知道。有人可以告诉我吗?

谢谢!

I have created this type:
create or replace type PRODTABLE as table of PROD_OBJ;

and I use that PRODTABLE in the follow PLSQL code:

    FUNCTION  INSERT_PRODUCTS (

      a_supplier_id IN FORNECEDOR.ID_FORNECEDOR%TYPE,

      a_prodArray IN PRODTABLE

      ) 

     RETURN NUMBER IS

        v_error_code    NUMBER;
        v_error_message VARCHAR2(255);
        v_result        NUMBER:= 0;
        v_prod_id       PRODUTO.ID_PROD%TYPE;
        v_supplier      FORNECEDOR%ROWTYPE;
        v_prodInserted  PROD_OBJ;
        type nestedTable is table of PROD_OBJ;
        newList   nestedTable := nestedTable();

     BEGIN

     SELECT  FORNEC_OBJ(ID_FORNECEDOR,NOME_FORNECEDOR,MORADA,ARMAZEM,EMAIL,TLF,TLM,FAX) into v_supplier from fornecedor where id_fornecedor = a_supplier_id;

      FOR i IN a_prodArray.FIRST .. a_prodArray.LAST LOOP

          INSERT INTO PRODUTO (PRODUTO.ID_PROD,PRODUTO.NOME_PROD,PRODUTO.PREC_COMPRA_PROD,PRODUTO.IVA_PROD,PRODUTO.PREC_VENDA_PROD,PRODUTO.QTD_STOCK_PROD,PRODUTO.QTD_STOCK_MIN_PROD) 
          VALUES (S_PRODUTO.nextval,a_prodArray(i).NOME_PROD,a_prodArray(i).PREC_COMPRA_PROD,a_prodArray(i).IVA_PROD,NULL,NULL,NULL);

          SELECT ID_PROD into v_prod_id from PRODUTO where NOME_PROD = a_prodArray(i).NOME_PROD;

          INSERT INTO PROD_FORNECIDO VALUES (a_supplier_id, v_prod_id,a_prodArray(i).PREC_COMPRA_PROD);

          SELECT PROD_OBJ(ID_PROD,NOME_PROD,PREC_COMPRA_PROD,PREC_VENDA_PROD,QTD_STOCK_PROD,QTD_STOCK_MIN_PROD,IVA_PROD) into v_prodInserted from PRODUTO where ID_PROD= v_prod_id;
          newList.extend;
          newList(newList.last):= v_prodinserted;

        END LOOP;

        /*the next line generates Error(43,63): PLS-00642: local collection types not      allowed in SQL statements,
Error(43,63): PL/SQL: ORA-00932: inconsistent datatypes: expected UDT got CHAR */
        INSERT INTO FORNECPRODS2 VALUES (a_supplier_id,v_supplier,newList);
        v_result:= 1; 
        RETURN v_result;
        COMMIT;
    (...)
    END;

I want to do something like a_prodArray(i):= v_prodInserted, but I can't because it's a nested table, so I need to retrieve another one to use in that insert OR update each element of nested table with the new inserted product. I have to do this because each prod_obj in PRODTABLE comes with an id=0, from JAVA.
Maybe there's another way, like making the default value for primary key equals SEQUENCE.nextval, I don't know. Could someone please enlight me?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假情假意假温柔 2024-08-24 14:04:44

您将无法修改 a_prodArray,因为它被声明为 IN 参数(因此不能用作赋值目标)。如果您将参数声明为 IN OUT,则分配成功:

SQL> CREATE TYPE PROD_OBJ AS OBJECT
  2  (
  3     ID_PROD   NUMBER,
  4     NOME_PROD VARCHAR2(1)
  5  )
  6  ;
  7  /     
Type created

SQL> CREATE TYPE PRODTABLE as table of PROD_OBJ;
  2  /     
Type created

SQL> CREATE FUNCTION INSERT_PRODUCTS(a_prodArray IN OUT PRODTABLE)
  2     RETURN NUMBER IS
  3  BEGIN
  4     a_prodArray(1) := prod_obj(1, NULL);
  5     RETURN 0;
  6  END;
  7  /     
Function created

SQL> DECLARE
  2     l_prod_table prodtable := prodtable();
  3     dummy NUMBER;
  4  BEGIN
  5     l_prod_table.extend();
  6     dummy := INSERT_PRODUCTS(l_prod_table);
  7  END;
  8  /     
PL/SQL procedure successfully completed

现在对于 PLS-00642 错误,您必须使用 EXACT 相同的数据类型作为表的定义。在这种情况下,我认为类型是 PRODTABLE SQL 类型,并且您必须将 newList 声明为 PRODTABLE(而不是 PLSQL 类型nestedTable) )。

you won't be able to modify a_prodArray since it is declared as an IN parameter (and therefore can not be used as an assignment target). IF you declare the parameter as an IN OUT, the assignment succeeds:

SQL> CREATE TYPE PROD_OBJ AS OBJECT
  2  (
  3     ID_PROD   NUMBER,
  4     NOME_PROD VARCHAR2(1)
  5  )
  6  ;
  7  /     
Type created

SQL> CREATE TYPE PRODTABLE as table of PROD_OBJ;
  2  /     
Type created

SQL> CREATE FUNCTION INSERT_PRODUCTS(a_prodArray IN OUT PRODTABLE)
  2     RETURN NUMBER IS
  3  BEGIN
  4     a_prodArray(1) := prod_obj(1, NULL);
  5     RETURN 0;
  6  END;
  7  /     
Function created

SQL> DECLARE
  2     l_prod_table prodtable := prodtable();
  3     dummy NUMBER;
  4  BEGIN
  5     l_prod_table.extend();
  6     dummy := INSERT_PRODUCTS(l_prod_table);
  7  END;
  8  /     
PL/SQL procedure successfully completed

Now for the PLS-00642 error, you have to use the EXACT same datatype as the definition of the table. In that case I suppose the type is the PRODTABLE SQL Type and you will have to declare newList as a PRODTABLE (and not a PLSQL type nestedTable).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文