循环遍历oracle中的嵌套表并将对象列表返回给java

发布于 2024-08-18 15:37:24 字数 2280 浏览 11 评论 0原文

我创建了这个表,如下所示:

    CREATE TABLE FORNECPRODS 
       (    SUPPLIER FORNEC_OBJ , 
        PRODUCTS PRODTABLE 
       )NESTED TABLE "PRODUCTS" STORE AS "PRODUCTSTABLE";
    /

    create or replace
    type PRODTABLE as table of PROD_OBJ;

    create or replace
    TYPE PROD_OBJ AS OBJECT (

       ID_PROD              NUMBER(6,0),
       NOME_PROD            VARCHAR2(100),        
       PREC_COMPRA_PROD     NUMBER(10,2),         
       PREC_VENDA_PROD      NUMBER(10,2),         
       QTD_STOCK_PROD       NUMBER(10),
       QTD_STOCK_MIN_PROD   NUMBER(10),
       IVA_PROD             NUMBER(6,2)
    );

/
create or replace
type PRODTABLE as table of PROD_OBJ;
/
create or replace type FORNEC_OBJ as object (
   ID_FORNECEDOR        NUMBER(6) ,
   NOME_FORNECEDOR      VARCHAR2(100) ,
   MORADA               VARCHAR2(300),
   ARMAZEM              VARCHAR2(300),
   EMAIL                VARCHAR2(30),
   TLF                  NUMBER(30) ,
   TLM                  NUMBER(30),
   FAX                  NUMBER(30)
   ); 
/

问题是我试图从表 FORNECPRODS 中获取每个供应商的产品列表,但我不能(我的想法是返回一个类似于散列的结构:list_of_products) 。为了尝试这一点,我仅使用此代码来打印每个供应商的产品:

declare
    v_products    prodtable;
    TYPE t_supplier is TABLE OF FORNEC_OBJ;
    v_supplier           t_supplier;

begin
    select supplier bulk collect into v_supplier from fornecprods;
    for j in v_supplier.first.. v_supplier.last
    loop
        select products into v_products
        from fornecprods where supplier = v_supplier(j);
        dbms_output.put_line('-----------------------');
        dbms_output.put_line('Products list of ' || v_supplier(j).NOME_FORNECEDOR);
        dbms_output.put_line('-----------------------');
        for i in v_products.first .. v_products.last
        loop
            dbms_output.put(v_products(i).NOME_PROD);
        end loop;
    end loop;
end;

但它没有返回第一个选择的任何数据。

那么,有人可以帮我找到一种从 oracle 到 java 检索列表(prodtable)的方法吗?我已经有了映射供应商和产品的类,我什至将它们的数组从 java 传递到了 oracle,所以它们很好,我只需要我的 j 树看起来像这样:

SUPPLIERS
->供应商1
-prod1
-prod2
- ...
->供应商2
-prod1
-prod2
- ....

是否可以使用我当前的表和类型检索所有信息,例如 supplier:list_of_products 的哈希值?

I have this table created as follow:

    CREATE TABLE FORNECPRODS 
       (    SUPPLIER FORNEC_OBJ , 
        PRODUCTS PRODTABLE 
       )NESTED TABLE "PRODUCTS" STORE AS "PRODUCTSTABLE";
    /

    create or replace
    type PRODTABLE as table of PROD_OBJ;

    create or replace
    TYPE PROD_OBJ AS OBJECT (

       ID_PROD              NUMBER(6,0),
       NOME_PROD            VARCHAR2(100),        
       PREC_COMPRA_PROD     NUMBER(10,2),         
       PREC_VENDA_PROD      NUMBER(10,2),         
       QTD_STOCK_PROD       NUMBER(10),
       QTD_STOCK_MIN_PROD   NUMBER(10),
       IVA_PROD             NUMBER(6,2)
    );

/
create or replace
type PRODTABLE as table of PROD_OBJ;
/
create or replace type FORNEC_OBJ as object (
   ID_FORNECEDOR        NUMBER(6) ,
   NOME_FORNECEDOR      VARCHAR2(100) ,
   MORADA               VARCHAR2(300),
   ARMAZEM              VARCHAR2(300),
   EMAIL                VARCHAR2(30),
   TLF                  NUMBER(30) ,
   TLM                  NUMBER(30),
   FAX                  NUMBER(30)
   ); 
/

The problem is that I'm trying to get a list of products for each supplier, from table FORNECPRODS, but I can't (my idea was to return a struct like an hash with :list_of_products). To try that, I used this code only to print products from each supplier:

declare
    v_products    prodtable;
    TYPE t_supplier is TABLE OF FORNEC_OBJ;
    v_supplier           t_supplier;

begin
    select supplier bulk collect into v_supplier from fornecprods;
    for j in v_supplier.first.. v_supplier.last
    loop
        select products into v_products
        from fornecprods where supplier = v_supplier(j);
        dbms_output.put_line('-----------------------');
        dbms_output.put_line('Products list of ' || v_supplier(j).NOME_FORNECEDOR);
        dbms_output.put_line('-----------------------');
        for i in v_products.first .. v_products.last
        loop
            dbms_output.put(v_products(i).NOME_PROD);
        end loop;
    end loop;
end;

but it returned no data found for the first select.

so, could someone please help me find a way to retrieve a list (prodtable) from oracle to java? I already have the class to map a supplier and a product, I even passed an array of each of them from java to oracle, so they're good, I only need my j-tree to look like this:

SUPPLIERS
->SUPPLIER1
-prod1
-prod2
- ...
->SUPPLIER2
-prod1
-prod2
- ....

Is it possible to retrieve all that info like an hash of supplier:list_of_products with my current table and types?

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

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

发布评论

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

评论(2

打小就很酷 2024-08-25 15:37:24

首先,我建议不要在数据库中永久存储嵌套表。虽然对象类型对于变量和临时结果非常有用,但您会发现嵌套表往往会使 SQL 查询变得复杂,增加开销,并且通常生成的代码比常规关系规范化设计更难维护。

现在对于您的问题,首先让我们填充您的表:

SQL> DECLARE
  2     l_prodtable prodtable := prodtable();
  3  BEGIN
  4     l_prodtable.extend(2);
  5     l_prodtable(1) := prod_obj(1, 'Prod A', '', '', '', '', '');
  6     l_prodtable(2) := prod_obj(2, 'Prod B', '', '', '', '', '');
  7     FOR i IN 1 .. 2 LOOP
  8        INSERT INTO fornecprods VALUES (fornec_obj(i, 'Forn '||i, '',
  9                                                   '', '', '', '', ''),
 10                                        l_prodtable);
 11     END LOOP;
 12  END;
 13  /     
PL/SQL procedure successfully completed

然后您将非常简单地循环遍历元素:

SQL> BEGIN
  2     FOR cc IN (SELECT supplier, products FROM fornecprods) LOOP
  3        dbms_output.put_line('-----------------------');
  4        dbms_output.put_line('Products list of '
  5                             || cc.supplier.NOME_FORNECEDOR);
  6        dbms_output.put_line('-----------------------');
  7        FOR i IN 1 .. cc.products.count LOOP
  8           dbms_output.put_line('-' || cc.products(i).NOME_PROD);
  9        END LOOP;
 10     END LOOP;
 11  END;
 12  /

-----------------------
Products list of Forn 1
-----------------------
-Prod A
-Prod B
-----------------------
Products list of Forn 2
-----------------------
-Prod A
-Prod B

PL/SQL procedure successfully completed

First I would suggest against permanently storing nested tables in the database. While objects types can be really useful for variables and temporary results, you will find that nested tables tend to complicate SQL queries, add overhead and in general produce code that is less maintainable than regular relational normalized designs.

Now for your problem, first let's populate your table:

SQL> DECLARE
  2     l_prodtable prodtable := prodtable();
  3  BEGIN
  4     l_prodtable.extend(2);
  5     l_prodtable(1) := prod_obj(1, 'Prod A', '', '', '', '', '');
  6     l_prodtable(2) := prod_obj(2, 'Prod B', '', '', '', '', '');
  7     FOR i IN 1 .. 2 LOOP
  8        INSERT INTO fornecprods VALUES (fornec_obj(i, 'Forn '||i, '',
  9                                                   '', '', '', '', ''),
 10                                        l_prodtable);
 11     END LOOP;
 12  END;
 13  /     
PL/SQL procedure successfully completed

You would then loop through the elements rather simply:

SQL> BEGIN
  2     FOR cc IN (SELECT supplier, products FROM fornecprods) LOOP
  3        dbms_output.put_line('-----------------------');
  4        dbms_output.put_line('Products list of '
  5                             || cc.supplier.NOME_FORNECEDOR);
  6        dbms_output.put_line('-----------------------');
  7        FOR i IN 1 .. cc.products.count LOOP
  8           dbms_output.put_line('-' || cc.products(i).NOME_PROD);
  9        END LOOP;
 10     END LOOP;
 11  END;
 12  /

-----------------------
Products list of Forn 1
-----------------------
-Prod A
-Prod B
-----------------------
Products list of Forn 2
-----------------------
-Prod A
-Prod B

PL/SQL procedure successfully completed
甜`诱少女 2024-08-25 15:37:24

@Vincent Malgrat:谢谢,你的建议对我真的很有用!我重新排列了我的代码并提出了这个函数:
创建或替换

function getAllSuppliersObjects RETURN FORNECTABLE AS
  suppliersList FORNECTABLE := FORNECTABLE();

BEGIN
     FOR res IN (SELECT supplier, products FROM fornecprods) LOOP
        /*dbms_output.put_line('-----------------------');
        dbms_output.put_line('Existent suppliers: '
                             || res.supplier.NOME_FORNECEDOR);*/
        suppliersList.extend;
        suppliersList(suppliersList.last):= res.supplier;
     END LOOP;
     return suppliersList;
  END;

@Vincent Malgrat: Thanks, your tip really worked for me! I rearranged my code and came up with this function:
create or replace

function getAllSuppliersObjects RETURN FORNECTABLE AS
  suppliersList FORNECTABLE := FORNECTABLE();

BEGIN
     FOR res IN (SELECT supplier, products FROM fornecprods) LOOP
        /*dbms_output.put_line('-----------------------');
        dbms_output.put_line('Existent suppliers: '
                             || res.supplier.NOME_FORNECEDOR);*/
        suppliersList.extend;
        suppliersList(suppliersList.last):= res.supplier;
     END LOOP;
     return suppliersList;
  END;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文