更新嵌套表中元素的属性
我创建了这种类型: 创建或替换类型 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将无法修改
a_prodArray
,因为它被声明为IN
参数(因此不能用作赋值目标)。如果您将参数声明为IN OUT
,则分配成功:现在对于
PLS-00642
错误,您必须使用 EXACT 相同的数据类型作为表的定义。在这种情况下,我认为类型是 PRODTABLE SQL 类型,并且您必须将 newList 声明为 PRODTABLE(而不是 PLSQL 类型nestedTable) )。you won't be able to modify
a_prodArray
since it is declared as anIN
parameter (and therefore can not be used as an assignment target). IF you declare the parameter as anIN OUT
, the assignment succeeds: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 thePRODTABLE
SQL Type and you will have to declare newList as aPRODTABLE
(and not a PLSQL typenestedTable
).