Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to create oracle dynamic object?

Posted on 2009-05-12
8
Medium Priority
?
463 Views
Last Modified: 2013-12-18
I want to pass dates dynamicatlly(hard coded now)  in the below query and I want to use the output of this query in another query's FROM clause as table object?
How can I achieve that can you please let me know.
I am usgin Oracle 10g.
SELECT   1 ord_er,
            'Opportunites' Market,
            NVL (SUM (DECODE (ldc_Code, 'COH', 1)), 0) coh,
            NVL (SUM (DECODE (ldc_Code, 'DEO', 1)), 0) deo,
            NVL (SUM (DECODE (ldc_Code, 'DKO', 1)), 0) dko,
            NVL (SUM (DECODE (ldc_Code, 'NFG', 1)), 0) nfg,
            NVL (SUM (DECODE (ldc_Code, 'NGY', 1)), 0) ngy,
            NVL (SUM (DECODE (ldc_Code, 'NIP', 1)), 0) nip,
            NVL (SUM (DECODE (ldc_Code, 'VDO', 1)), 0) vdo,
            NVL (SUM (DECODE (ldc_code, 'XXX', 0, 1)), 0) Total
     FROM   ecare_winback_Tb
    WHERE   TRUNC (created_Date) BETWEEN TO_DATE ('27-APR-09', 'DD-MON-YY')
                                         - 7
                                     AND  TO_DATE ('27-APR-09', 'DD-MON-YY')
                                          - 1
   UNION
     SELECT   DECODE (wb_bucket,
                      'OPPO',
                      6,
                      'WINB',
                      2,
                      'LOST',
                      3,
                      'DEAD',
                      4)
                 ord_er,
              wb_bucket Market,
              NVL (SUM (DECODE (ldc_Code, 'COH', 1)), 0) coh,
              NVL (SUM (DECODE (ldc_Code, 'DEO', 1)), 0) deo,
              NVL (SUM (DECODE (ldc_Code, 'DKO', 1)), 0) dko,
              NVL (SUM (DECODE (ldc_Code, 'NFG', 1)), 0) nfg,
              NVL (SUM (DECODE (ldc_Code, 'NGY', 1)), 0) ngy,
              NVL (SUM (DECODE (ldc_Code, 'NIP', 1)), 0) nip,
              NVL (SUM (DECODE (ldc_Code, 'VDO', 1)), 0) vdo,
              NVL (SUM (DECODE (ldc_code, 'XXX', 0, 1)), 0) Total
       FROM   ecare_winback_Tb
      WHERE   TRUNC (created_Date) BETWEEN TO_DATE ('27-APR-09', 'DD-MON-YY')
                                           - 7
                                       AND  TO_DATE ('27-APR-09', 'DD-MON-YY')
                                            - 1
   GROUP BY   wb_bucket
   UNION
   SELECT   5 ord_er,
            'Completed' Market,
            NVL (SUM (DECODE (ldc_Code, 'COH', 1)), 0) coh,
            NVL (SUM (DECODE (ldc_Code, 'DEO', 1)), 0) deo,
            NVL (SUM (DECODE (ldc_Code, 'DKO', 1)), 0) dko,
            NVL (SUM (DECODE (ldc_Code, 'NFG', 1)), 0) nfg,
            NVL (SUM (DECODE (ldc_Code, 'NGY', 1)), 0) ngy,
            NVL (SUM (DECODE (ldc_Code, 'NIP', 1)), 0) nip,
            NVL (SUM (DECODE (ldc_Code, 'VDO', 1)), 0) vdo,
            NVL (SUM (DECODE (ldc_code, 'XXX', 0, 1)), 0) Total
     FROM   ecare_winback_Tb
    WHERE   TRUNC (created_Date) BETWEEN TO_DATE ('27-APR-09', 'DD-MON-YY')
                                         - 7
                                     AND  TO_DATE ('27-APR-09', 'DD-MON-YY')
                                          - 1
            AND wb_bucket != 'OPPO'
   ORDER BY   1;

Open in new window

0
Comment
Question by:GouthamAnand
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 24366296
It all depends on how you are executing this query.
There are many different options, but one of the easiest is to create a procedure which accepts the date parameters and writes the query results to a temporary table which then is accessed by the other query.
0
 

Author Comment

by:GouthamAnand
ID: 24366570
here temporary table is type object? I am able to create as

create or replace PROCEDURE ECARE.win_TEST_PRC(p_date1 in varchar2, p_date2 in varchar2)
AS
CURSOR c1
   IS    
SELECT   1 ord_er,
            'Opportunites' Market,
            NVL (SUM (DECODE (ldc_Code, 'COH', 1)), 0) coh,
            NVL (SUM (DECODE (ldc_Code, 'DEO', 1)), 0) deo,
            NVL (SUM (DECODE (ldc_Code, 'DKO', 1)), 0) dko,
            NVL (SUM (DECODE (ldc_Code, 'NFG', 1)), 0) nfg,
            NVL (SUM (DECODE (ldc_Code, 'NGY', 1)), 0) ngy,
            NVL (SUM (DECODE (ldc_Code, 'NIP', 1)), 0) nip,
            NVL (SUM (DECODE (ldc_Code, 'VDO', 1)), 0) vdo,
            NVL (SUM (DECODE (ldc_code, 'XXX', 0, 1)), 0) Total
     FROM   ecare_winback_Tb
    WHERE   TRUNC (created_Date) BETWEEN TO_DATE (p_date1, 'DD-MON-YY')
                                         - 7
                                     AND  TO_DATE (p_date2, 'DD-MON-YY')
                                          - 1
   UNION
     SELECT   DECODE (wb_bucket,
                      'OPPO',
                      6,
                      'WINB',
                      2,
                      'LOST',
                      3,
                      'DEAD',
                      4)
                 ord_er,
              wb_bucket Market,
              NVL (SUM (DECODE (ldc_Code, 'COH', 1)), 0) coh,
              NVL (SUM (DECODE (ldc_Code, 'DEO', 1)), 0) deo,
              NVL (SUM (DECODE (ldc_Code, 'DKO', 1)), 0) dko,
              NVL (SUM (DECODE (ldc_Code, 'NFG', 1)), 0) nfg,
              NVL (SUM (DECODE (ldc_Code, 'NGY', 1)), 0) ngy,
              NVL (SUM (DECODE (ldc_Code, 'NIP', 1)), 0) nip,
              NVL (SUM (DECODE (ldc_Code, 'VDO', 1)), 0) vdo,
              NVL (SUM (DECODE (ldc_code, 'XXX', 0, 1)), 0) Total
       FROM   ecare_winback_Tb
      WHERE   TRUNC (created_Date) BETWEEN TO_DATE ('27-APR-09', 'DD-MON-YY')
                                           - 7
                                       AND  TO_DATE ('27-APR-09', 'DD-MON-YY')
                                            - 1
   GROUP BY   wb_bucket
   UNION
   SELECT   5 ord_er,
            'Completed' Market,
            NVL (SUM (DECODE (ldc_Code, 'COH', 1)), 0) coh,
            NVL (SUM (DECODE (ldc_Code, 'DEO', 1)), 0) deo,
            NVL (SUM (DECODE (ldc_Code, 'DKO', 1)), 0) dko,
            NVL (SUM (DECODE (ldc_Code, 'NFG', 1)), 0) nfg,
            NVL (SUM (DECODE (ldc_Code, 'NGY', 1)), 0) ngy,
            NVL (SUM (DECODE (ldc_Code, 'NIP', 1)), 0) nip,
            NVL (SUM (DECODE (ldc_Code, 'VDO', 1)), 0) vdo,
            NVL (SUM (DECODE (ldc_code, 'XXX', 0, 1)), 0) Total
     FROM   ecare_winback_Tb
    WHERE   TRUNC (created_Date) BETWEEN TO_DATE ('27-APR-09', 'DD-MON-YY')
                                         - 7
                                     AND  TO_DATE ('27-APR-09', 'DD-MON-YY')
                                          - 1
            AND wb_bucket != 'OPPO'
   ORDER BY   1;  
   
   
 
   TYPE winback_obj IS
      TABLE OF  c1%ROWTYPE INDEX BY PLS_INTEGER;
     
       l_winback_obj winback_obj;

     
BEGIN
OPEN c1;
   LOOP
      FETCH c1
         BULK COLLECT INTO l_winback_obj;
    END LOOP;  

close c1;

     
END win_TEST_PRC;
/

I want to pass this l_winback_obj to another query as

select * from TABLE(l_winback_obj) --SOMETHING LIKE THIS

Can you please let me know How can I do that?
0
 

Author Comment

by:GouthamAnand
ID: 24366772
How Can I select values from the object table type?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 1500 total points
ID: 24367607
Try something like this:
 

/* Formatted on 2009/05/12 14:15 (Formatter Plus v4.8.7) */
CREATE OR REPLACE TYPE testobjtype AS OBJECT (
   ord_er   NUMBER (9),
   market   VARCHAR2 (32),
   coh      NUMBER (13),
   deo      NUMBER (13),
   dko      NUMBER (13),
   nfg      NUMBER (13),
   ngy      NUMBER (13),
   nip      NUMBER (13),
   vdo      NUMBER (13),
   total    NUMBER (33)
)
/
 
CREATE OR REPLACE TYPE testtabletype AS TABLE OF testobjtype
/
 
CREATE OR REPLACE FUNCTION ecare.win_test_prc (
   p_date1   IN   VARCHAR2,
   p_date2   IN   VARCHAR2
)
   RETURN testtabletype
AS
   CURSOR c1
   IS
      SELECT   1 ord_er, 'Opportunites' market,
               NVL (SUM (DECODE (ldc_code, 'COH', 1)), 0) coh,
               NVL (SUM (DECODE (ldc_code, 'DEO', 1)), 0) deo,
               NVL (SUM (DECODE (ldc_code, 'DKO', 1)), 0) dko,
               NVL (SUM (DECODE (ldc_code, 'NFG', 1)), 0) nfg,
               NVL (SUM (DECODE (ldc_code, 'NGY', 1)), 0) ngy,
               NVL (SUM (DECODE (ldc_code, 'NIP', 1)), 0) nip,
               NVL (SUM (DECODE (ldc_code, 'VDO', 1)), 0) vdo,
               NVL (SUM (DECODE (ldc_code, 'XXX', 0, 1)), 0) total
          FROM ecare_winback_tb
         WHERE TRUNC (created_date) BETWEEN TO_DATE (p_date1, 'DD-MON-YY')
                                            - 7
                                        AND TO_DATE (p_date2, 'DD-MON-YY')
                                            - 1
      UNION
      SELECT   DECODE (wb_bucket,
                       'OPPO', 6,
                       'WINB', 2,
                       'LOST', 3,
                       'DEAD', 4
                      ) ord_er,
               wb_bucket market,
               NVL (SUM (DECODE (ldc_code, 'COH', 1)), 0) coh,
               NVL (SUM (DECODE (ldc_code, 'DEO', 1)), 0) deo,
               NVL (SUM (DECODE (ldc_code, 'DKO', 1)), 0) dko,
               NVL (SUM (DECODE (ldc_code, 'NFG', 1)), 0) nfg,
               NVL (SUM (DECODE (ldc_code, 'NGY', 1)), 0) ngy,
               NVL (SUM (DECODE (ldc_code, 'NIP', 1)), 0) nip,
               NVL (SUM (DECODE (ldc_code, 'VDO', 1)), 0) vdo,
               NVL (SUM (DECODE (ldc_code, 'XXX', 0, 1)), 0) total
          FROM ecare_winback_tb
         WHERE TRUNC (created_date) BETWEEN TO_DATE (p_date1, 'DD-MON-YY')
                                            - 7
                                        AND TO_DATE (p_date2, 'DD-MON-YY')
                                            - 1
      GROUP BY wb_bucket
      UNION
      SELECT   5 ord_er, 'Completed' market,
               NVL (SUM (DECODE (ldc_code, 'COH', 1)), 0) coh,
               NVL (SUM (DECODE (ldc_code, 'DEO', 1)), 0) deo,
               NVL (SUM (DECODE (ldc_code, 'DKO', 1)), 0) dko,
               NVL (SUM (DECODE (ldc_code, 'NFG', 1)), 0) nfg,
               NVL (SUM (DECODE (ldc_code, 'NGY', 1)), 0) ngy,
               NVL (SUM (DECODE (ldc_code, 'NIP', 1)), 0) nip,
               NVL (SUM (DECODE (ldc_code, 'VDO', 1)), 0) vdo,
               NVL (SUM (DECODE (ldc_code, 'XXX', 0, 1)), 0) total
          FROM ecare_winback_tb
         WHERE TRUNC (created_date) BETWEEN TO_DATE (p_date1, 'DD-MON-YY')
                                            - 7
                                        AND TO_DATE (p_date2, 'DD-MON-YY')
                                            - 1
           AND wb_bucket != 'OPPO'
      ORDER BY 1;
 
   l_winback_obj   testtabletype := testtabletype ();
BEGIN
   OPEN c1;
 
   FETCH c1
     BULK COLLECT INTO l_winback_obj;
 
   CLOSE c1;
END win_test_prc;
/
-- -------------------------------------------
SELECT *
  FROM THE (SELECT CAST (ecare.win_test_prc ('27-APR-09', '27-APR-09') AS mytabletype)
              FROM DUAL) a
/

Open in new window

0
 

Author Comment

by:GouthamAnand
ID: 24368209
I am getting this error.
PLS-00386: type mismatch found at 'L_WINBACK_OBJ' between FETCH cursor and INTO variables
0
 

Author Comment

by:GouthamAnand
ID: 24368295
Also I want to use like

select * from (select * from XXXX--here I want to use the object l_winback_obj
where XXX --- here I want to use the one column of the object l_winback_obj
 = 'Opportunites')
0
 

Author Comment

by:GouthamAnand
ID: 24368420
Actually data is like
ORD_ER      MARKET      COH      DEO      DKO      NFG      NGY      NIP      VDO      TOTAL
1      Opportunites      24      652      0      18      55      19      231      999
2      WINB      2      52      0      2      5      1      18      80

I do not know why I am getting
PLS-00386: type mismatch found at 'L_WINBACK_OBJ' between FETCH cursor and INTO variables

I use pl/sql table types then I can not give the return table type for the function, I think. So can we use the database table types as you given in the example?
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 24374579
OK, BULK-COLLECT is not working, try this:
 

CREATE OR REPLACE TYPE testobjtype AS OBJECT (
   ord_er   NUMBER (9),
   market   VARCHAR2 (32),
   coh      NUMBER (13),
   deo      NUMBER (13),
   dko      NUMBER (13),
   nfg      NUMBER (13),
   ngy      NUMBER (13),
   nip      NUMBER (13),
   vdo      NUMBER (13),
   total    NUMBER (33)
)
/
 
CREATE OR REPLACE TYPE testtabletype AS TABLE OF testobjtype
/
 
CREATE OR REPLACE FUNCTION ecare.win_test_prc (
   p_date1   IN   VARCHAR2,
   p_date2   IN   VARCHAR2
)
   RETURN testtabletype
AS
   CURSOR c1
   IS
      SELECT   1 ord_er, 'Opportunites' market,
               NVL (SUM (DECODE (ldc_code, 'COH', 1)), 0) coh,
               NVL (SUM (DECODE (ldc_code, 'DEO', 1)), 0) deo,
               NVL (SUM (DECODE (ldc_code, 'DKO', 1)), 0) dko,
               NVL (SUM (DECODE (ldc_code, 'NFG', 1)), 0) nfg,
               NVL (SUM (DECODE (ldc_code, 'NGY', 1)), 0) ngy,
               NVL (SUM (DECODE (ldc_code, 'NIP', 1)), 0) nip,
               NVL (SUM (DECODE (ldc_code, 'VDO', 1)), 0) vdo,
               NVL (SUM (DECODE (ldc_code, 'XXX', 0, 1)), 0) total
          FROM ecare_winback_tb
         WHERE TRUNC (created_date) BETWEEN TO_DATE (p_date1, 'DD-MON-YY')
                                            - 7
                                        AND TO_DATE (p_date2, 'DD-MON-YY')
                                            - 1
      UNION
      SELECT   DECODE (wb_bucket,
                       'OPPO', 6,
                       'WINB', 2,
                       'LOST', 3,
                       'DEAD', 4
                      ) ord_er,
               wb_bucket market,
               NVL (SUM (DECODE (ldc_code, 'COH', 1)), 0) coh,
               NVL (SUM (DECODE (ldc_code, 'DEO', 1)), 0) deo,
               NVL (SUM (DECODE (ldc_code, 'DKO', 1)), 0) dko,
               NVL (SUM (DECODE (ldc_code, 'NFG', 1)), 0) nfg,
               NVL (SUM (DECODE (ldc_code, 'NGY', 1)), 0) ngy,
               NVL (SUM (DECODE (ldc_code, 'NIP', 1)), 0) nip,
               NVL (SUM (DECODE (ldc_code, 'VDO', 1)), 0) vdo,
               NVL (SUM (DECODE (ldc_code, 'XXX', 0, 1)), 0) total
          FROM ecare_winback_tb
         WHERE TRUNC (created_date) BETWEEN   TO_DATE ('27-APR-09',
                                                       'DD-MON-YY'
                                                      )
                                            - 7
                                        AND   TO_DATE ('27-APR-09',
                                                       'DD-MON-YY'
                                                      )
                                            - 1
      GROUP BY wb_bucket
      UNION
      SELECT   5 ord_er, 'Completed' market,
               NVL (SUM (DECODE (ldc_code, 'COH', 1)), 0) coh,
               NVL (SUM (DECODE (ldc_code, 'DEO', 1)), 0) deo,
               NVL (SUM (DECODE (ldc_code, 'DKO', 1)), 0) dko,
               NVL (SUM (DECODE (ldc_code, 'NFG', 1)), 0) nfg,
               NVL (SUM (DECODE (ldc_code, 'NGY', 1)), 0) ngy,
               NVL (SUM (DECODE (ldc_code, 'NIP', 1)), 0) nip,
               NVL (SUM (DECODE (ldc_code, 'VDO', 1)), 0) vdo,
               NVL (SUM (DECODE (ldc_code, 'XXX', 0, 1)), 0) total
          FROM ecare_winback_tb
         WHERE TRUNC (created_date) BETWEEN   TO_DATE ('27-APR-09',
                                                       'DD-MON-YY'
                                                      )
                                            - 7
                                        AND   TO_DATE ('27-APR-09',
                                                       'DD-MON-YY'
                                                      )
                                            - 1
           AND wb_bucket != 'OPPO'
      ORDER BY 1;
 
   l_winback_obj   testtabletype := testtabletype ();
BEGIN
--OPEN c1;
--FETCH c1
--    BULK COLLECT INTO l_winback_obj;
--close c1;
   FOR x IN c1
   LOOP
      l_winback_obj.EXTEND;
      l_winback_obj (l_winback_obj.COUNT) :=
           testobjtype (x.ord_er,
                        x.market,
                        x.coh,
                        x.deo,
                        x.dko,
                        x.nfg,
                        x.ngy,
                        x.nip,
                        x.vdo,
                        x.total
                       );
   END LOOP;
 
   RETURN l_winback_obj;
END win_test_prc;
/
-- -------------------------------------------
SELECT *
  FROM THE (SELECT CAST (ecare.win_test_prc ('27-APR-09', '27-APR-09') AS mytabletype)
              FROM DUAL) a
/

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Via a live example, show how to take different types of Oracle backups using RMAN.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question