[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

10/13/2009 at 01:27PM PDT, ID: 24809186 | Points: 500
[x]
Attachment Details

Oracle 11g PL/SQL Ref Cursor useage

Asked by GNOVAK in PL / SQL, Oracle 11.x Database, Oracle 10.x, Oracle Database, SQL Query Syntax

Tags: pl/sql, oracle 11g

I'm attempting to write a generic procedure that will aggregate a column and  depost the results in another column.
I thought that a REF CURSOR would be the answer since I do not know how to create a cursor on the fly.
Since I didn't know a whole lot about that, I started with the code below.   I would have a function where everything is passed and it would, in turn send the cursor to the procedure to do the actual aggregation.

When I looked up REF CURSOR, I noticed I had to define the vars. Since I dont know the table that will be utilized (I'm trying to create a generic routine), I ran into a problem.

Is there any way to do what I'm attempting to do?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
PROCEDURE ACCUMULATOR_WRAP( TABLE_NAME      IN VARCHAR2,
                            PK_ID           IN VARCHAR2,
                            ORDER_BY_1      IN VARCHAR2,
                            ORDER_BY_2      IN VARCHAR2,
                            ORDER_BY_3      IN VARCHAR2 :=NULL,
                            ORDER_BY_4      IN VARCHAR2 :=NULL,
                            DATE_FROM       IN DATE     :=NULL,
                            DATE_TO         IN DATE     :=NULL,
                            DATE_COL        IN VARCHAR2 :=NULL,
                            AMT_TO_ACCUM    IN NUMBER,
                            COL_TO_STORE    IN VARCHAR2) IS
--TABLE_NAME    - Table where fields are located in
--PK_ID         - Primary Key Field Name
--ORDER_BY_1    - Primary Field Name to be accumulated by (ie, Member_ID)
--ORDER_BY_2    - Secondary order by field (ie, Date Processed)
--ORDER_BY_3    - Tertiary order by field (ie Claim Number) OPTIONAL
--ORDER_BY_4    - Final order by field    OPTIONAL
--DATE_FROM     - Start of Date Range (DATE VALUE) 
--DATE_TO       - Last Date in Date Range 
--DATE_COL      - Field Name containing To Date 
--AMT_TO_ACCUM  - The field name to be accumulated - MUST BE NUMBER!
--COL_TO_STORE  - Field Name in which to store accumulation
 
 
--SET UP REFCURSOR FROM SPECS                                                        
G_CURSOR R_CURSOR;
S_CURSOR    VARCHAR2(4000);
 
 
 
BEGIN
 
S_CURSOR := 'SELECT '   || ORDER_BY_1 ||',' || ORDER_BY_2;
IF ORDER_BY_3 IS NOT NULL
THEN
    S_CURSOR := S_CURSOR || ORDER_BY_3;
END IF;
IF ORDER_BY_4 IS NOT NULL
THEN
    S_CURSOR := S_CURSOR || ORDER_BY_4;
END IF;
 
S_CURSOR := S_CURSOR ||' FROM ' || TABLE_NAME;
 
-- DATES
IF DATE_FROM IS NOT NULL AND DATE_COL IS NOT NULL
THEN
    IF DATE_TO IS NOT NULL
    THEN
        S_CURSOR := S_CURSOR || ' WHERE ' || DATE_COL ||'BETWEEN ';
        S_CURSOR := S_CURSOR || DATE_FROM || ' AND ' || DATE_TO;   
    ELSE
        S_CURSOR := S_CURSOR || ' WHERE ' || DATE_COL || ' > = ';
        S_CURSOR := S_CURSOR || DATE_FROM ;
    END IF;
ELSE
    IF DATE_TO IS NOT NULL
    THEN
        S_CURSOR := S_CURSOR || ' WHERE ' || DATE_COL || '=<';
        S_CURSOR := S_CURSOR || DATE_FROM;
    END IF;    
END IF;    
S_CURSOR := S_CURSOR || ' ORDER BY ' || ORDER_BY_1 ||',' || ORDER_BY_2;
IF ORDER_BY_3 IS NOT NULL
THEN
    S_CURSOR := S_CURSOR || ORDER_BY_3;
END IF;
IF ORDER_BY_4 IS NOT NULL
THEN
    S_CURSOR := S_CURSOR || ORDER_BY_4;
END IF;
[+][-]10/13/09 11:46 PM, ID: 25567818

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/14/09 06:02 AM, ID: 25569941

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/15/09 12:18 AM, ID: 25578033

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/21/09 06:39 AM, ID: 25623780

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]10/21/09 06:39 AM, ID: 25623784

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]10/21/09 06:55 AM, ID: 25623972

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/21/09 07:38 AM, ID: 25624408

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/21/09 07:43 AM, ID: 25624471

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/18/09 05:40 PM, ID: 25856901

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20090824-EE-VQP-74 - Hierarchy / EE_QW_3_20080625