Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old September 20th, 2004, 11:48 AM
million1977 million1977 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Aberdeen, Scotland
Posts: 48 million1977 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 51 m 3 sec
Reputation Power: 5
PL/SQL complicated dynamic cursor query produces error

Hi,

I'm a newbie to PL/SQL but have been thrown in at the deep end, having to make changes to a large piece of software.
Part of this involves me changing a static cursor (which runs a huge query) to a dynamic one so that the field to order by is decided at run-time. The code is as follows:

Code:
// Declarations- sorry, huge query I know

v_all_nom_list VARCHAR2(32767) :=
    'SELECT   n.obj_id                   obj_id,
             n.telex_id                 telex_id,
             f.facility_desc            facility_desc,
             s.shipper_desc             shipper_desc,
             l.lk_desc                  nomination_type,
             n.effective_hour           effective_hour,
             n.quantity                 quantity,
             NVL( n.approved_ind, 0 )   approved_ind,
             NVL( n.verified_ind, 0 )   verified_ind,
             NVL( n.superseded_ind, 0 ) superseded_ind,
             NVL( n.agg_ind, 0 )        agg_ind,
             NVL( n.rejected_ind, 0)    rejected_ind
    FROM     t_lookup               l,
             t_nom                  n,
             t_facility             f,
             t_shipper              s,
             t_contract             c 
    WHERE    p_cust_util.get_gas_day( n.effective_day, n.effective_hour )
                                  = DECODE( :in_reference_id,
                                            NULL, :in_gas_day,
                                            p_cust_util.get_gas_day( n.effective_day, n.effective_hour ) )
    AND      c.contract_type_code = p_cust_con.con_CAPACITY
    AND      c.from_obj_type_code = p_cust_con.con_SHIPPER
    AND      c.from_obj_id        = DECODE( :in_reference_id,
                                            NULL, DECODE( :in_customer_id,
                                                          NULL, c.from_obj_id,
                                                          :in_customer_id ),
                                            c.from_obj_id )
    AND      c.to_obj_type_code   = p_cust_con.con_FACILITY
    AND      c.to_obj_id          = DECODE( :in_reference_id,
                                            NULL, DECODE( :in_site_id,
                                                          NULL, c.to_obj_id,
                                                          :in_site_id ),
                                            c.to_obj_id )
    AND      UPPER( n.telex_id )  = DECODE( c_:in_reference_id,
                                            NULL, UPPER(n.telex_id),
                                            UPPER( :in_reference_id ) )
    AND      l.type_code          = p_cust_con.con_NOM_TYPE
    AND      n.nomination_type    = l.lk_code
    AND      c.from_obj_id        = s.obj_id
    AND      c.to_obj_id          = f.obj_id
    AND      c.obj_id             = n.node_obj_id
    AND      n.scenario_id        = p_cust_con.con_STORIT
    AND      n.scenario_type      = p_cust_con.con_EOD
    AND      n.node_type_code     = p_cust_con.con_CONTRACT
    AND      n.nomination_type    = DECODE( :in_reference_id,
                                            NULL, :in_nom_type,
                                            n.nomination_type ) ORDER BY ';

TYPE t_nom_rec IS RECORD (obj_id           t_nom.obj_id%TYPE,
                            telex_id         t_nom.telex_id%TYPE,
                            facility_desc    t_facility.facility_desc%TYPE, 
                            shipper_desc     t_shipper.shipper_desc%TYPE, 
                            nomination_type  t_lookup.lk_desc%TYPE, 
                            effective_hour   t_nom.effective_hour%TYPE,    
                            quantity         t_nom.quantity%TYPE,
                            approved_ind     t_nom.approved_ind%TYPE,
                            verified_ind     t_nom.verified_ind%TYPE,
                            superseded_ind   t_nom.superseded_ind%TYPE,
                            agg_ind          t_nom.agg_ind%TYPE,
                            rejected_ind     t_nom.rejected_ind%TYPE);
  TYPE t_nom_tab IS TABLE OF t_nom_rec INDEX BY BINARY_INTEGER;
  v_nom_tab t_nom_tab;
  TYPE t_nom_cur IS REF CURSOR;
  c_nom_details t_nom_cur;
  nom_rec t_nom_rec;

// In main body:

      FOR i IN 1..v_nom_type.COUNT LOOP   
         htp.prn(v_user_nom_list);
         OPEN c_nom_details FOR v_all_nom_list USING in_reference,
                                       v_gas_day,
                                       in_reference,
                                       v_cust_obj,
                                       v_cust_obj,
                                       in_reference,
                                       v_site_obj,
                                       v_site_obj,
                                       in_reference,
                                       in_reference,
                                       in_reference,
                                       v_nom_type(i);
         LOOP
            v_err_point := '20.52';
            FETCH c_nom_details INTO nom_rec;
            EXIT WHEN c_nom_details%NOTFOUND;
            v_err_point := '20.53';
            v_nom_tab( v_count ) := nom_rec;
            v_count := v_count + 1;
         END LOOP;
         CLOSE c_nom_details;
      END LOOP;


This compiles fine, but produces the error 'ORA-00907: missing right parenthesis' at run-time. This seems to occur at the line that OPENs the cursor. However, I've checked through the string that contains the query several times and can't see a problem with it- there's the same number of opening and closing brackets, and they all seem to be in the right place.
As this is the first time I've attempted to code something like this, I was hoping that somebody might spot something obvious that I've done wrong.

Any help would be very much appreciated!

Reply With Quote
  #2  
Old September 21st, 2004, 06:19 AM
Ankur Gupta Ankur Gupta is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 5 Ankur Gupta User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi,

Dont panic, its a normal situation..
Your query is having some error. I have faced this problem several times while working with ref cursors.
Brackets in the query are ok i've checked them using a macro, you need to replace hardcoded values in the query and try to execute it, then only you'll be able to debug the problem.
Make a habit of executing query before opening it for ref cursor.

Hope you'll get it working now.

Regards
Ankur

Reply With Quote
  #3  
Old September 21st, 2004, 11:21 AM
million1977 million1977 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Aberdeen, Scotland
Posts: 48 million1977 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 51 m 3 sec
Reputation Power: 5
Ankur,

many thanks for your reply.

I've sorted the problem. In case anybody has a similar problem, the cause of it was this:

When the SELECT statement in the string is executed dynamically at run-time, it cannot access the PL/SQL variables or constants, whether they are in the same package or not (unlike the case if the SELECT was just declared in a static cursor).
My query included constants in another package, such as p_cust_con.con_STORIT. It still didn't work when I declared them in the current package.

What you have to do is pass the values of any PL/SQL constants/variables as parameters, thus making the query in the string 'pure SQL', with no references to any PL/SQL.

Working code:

Code:
--In declaration section

v_all_nom_list VARCHAR2(32767) :=
    'SELECT   n.obj_id                   obj_id,
             n.telex_id                 telex_id,
             f.facility_desc            facility_desc,
             s.shipper_desc             shipper_desc,
             l.lk_desc                  nomination_type,
             n.effective_hour           effective_hour,
             n.quantity                 quantity,
             NVL( n.approved_ind, 0 )   approved_ind,
             NVL( n.verified_ind, 0 )   verified_ind,
             NVL( n.superseded_ind, 0 ) superseded_ind,
             NVL( n.agg_ind, 0 )        agg_ind,
             NVL( n.rejected_ind, 0)    rejected_ind
    FROM     t_lookup               l,
             t_nom                  n,
             t_facility             f,
             t_shipper              s,
             t_contract             c 
    WHERE    p_cust_util.get_gas_day( n.effective_day, n.effective_hour )
                                  = DECODE( :in_reference_id,
                                            NULL, :in_gas_day,
                                            p_cust_util.get_gas_day( n.effective_day, n.effective_hour ) )
    AND      c.contract_type_code = :con_CAPACITY
    AND      c.from_obj_type_code = :con_SHIPPER
    AND      c.from_obj_id        = DECODE( :in_reference_id,
                                            NULL, DECODE( :in_customer_id,
                                                          NULL, c.from_obj_id,
                                                          :in_customer_id ),
                                            c.from_obj_id )
    AND      c.to_obj_type_code   = :con_FACILITY
    AND      c.to_obj_id          = DECODE( :in_reference_id,
                                            NULL, DECODE( :in_site_id,
                                                          NULL, c.to_obj_id,
                                                          :in_site_id ),
                                            c.to_obj_id )
    AND      UPPER( n.telex_id )  = DECODE( :in_reference_id,
                                            NULL, UPPER(n.telex_id),
                                            UPPER( :in_reference_id ) )
    AND      l.type_code          = :con_NOM_TYPE
    AND      n.nomination_type    = l.lk_code
    AND      c.from_obj_id        = s.obj_id
    AND      c.to_obj_id          = f.obj_id
    AND      c.obj_id             = n.node_obj_id
    AND      n.scenario_id        = :con_STORIT
    AND      n.scenario_type      = :con_EOD
    AND      n.node_type_code     = :con_CONTRACT
    AND      n.nomination_type    = DECODE( :in_reference_id,
                                            NULL, :in_nom_type,
                                            n.nomination_type ) ORDER BY '; --nb order by bit is added at runtime

-- In main body
IF ( p_cust_sec_shipper_***.isOperator( in_out_user ) ) THEN
      v_err_point := '20.51';
      FOR i IN 1..v_nom_type.COUNT LOOP   
         OPEN c_nom_details FOR v_all_nom_list USING in_reference,
                                       v_gas_day,
                                       p_cust_con.con_CAPACITY,
                                       p_cust_con.con_SHIPPER,
                                       in_reference,
                                       v_cust_obj,
                                       v_cust_obj,
                                       p_cust_con.con_FACILITY,
                                       in_reference,
                                       v_site_obj,
                                       v_site_obj,
                                       in_reference,
                                       in_reference,
                                       p_cust_con.con_NOM_TYPE,
                                       p_cust_con.con_STORIT,
                                       p_cust_con.con_EOD,
                                       p_cust_con.con_CONTRACT,
                                       in_reference,
                                       v_nom_type(i);
         LOOP
            v_err_point := '20.52';
            FETCH c_nom_details INTO nom_rec;
            EXIT WHEN c_nom_details%NOTFOUND;
            v_err_point := '20.53';
            v_nom_tab( v_count ) := nom_rec;
            v_count := v_count + 1;
         END LOOP;
         CLOSE c_nom_details;
      END LOOP;


Hope this of use to some of you

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > PL/SQL complicated dynamic cursor query produces error


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway