Advertisement

02.26.2008 at 06:37AM PST, ID: 23193575
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

FORALL.... SELECT syntax on Oracle 10g release 2

Tags: oracle forall bulk collection sql plsql
Hi Experts,

I have been asked to update a stored procedure by our application vendor and when I load the supplied code in Toad I get a syntax error.  A selection of the code being run is as follows:

CREATE OR REPLACE PROCEDURE DBO.Usp_Archive_Stop
    (in_vchWhId                t_whse.wh_id%TYPE,
     in_vchStopID              t_wol_stop_shipped.stop_number%TYPE,
     in_dteShippedDate         t_wol_stop_shipped.shipped_date%TYPE,
     in_vchProcessFlag         t_wol_stop_shipped.process_flag%TYPE,
     out_vchErrorNbr     OUT   VARCHAR2,
     out_vchErrorMsg     OUT   VARCHAR2)
AS
...
...
    TYPE tab_OrderNumber IS TABLE OF t_order.order_number%TYPE
    INDEX BY PLS_INTEGER;

  v_tblOrderNumber          tab_OrderNumber;
  v_vchWaveId               t_wave_master.wave_id%TYPE;
  j                                   PLS_INTEGER;
...
...
BEGIN
    -- Delete AFO Wave Detail .
    FORALL j IN v_tblOrderNumber.FIRST .. v_tblOrderNumber.LAST
      SELECT wave_id
        INTO v_vchWaveId
        FROM t_afo_wave_detail
       WHERE wh_id = in_vchWhId
         AND order_number = v_tblOrderNumber(j)
         AND load_seq = in_vchStopId;

      DELETE
        FROM t_afo_wave_detail
       WHERE wh_id = in_vchWhId
         AND order_number = v_tblOrderNumber(j)
         AND load_seq = in_vchStopId;

    EXCEPTION
      WHEN OTHERS THEN
        out_vchErrorNbr := '-20036';
        out_vchErrorMsg := SQLCODE||' - '||SQLERRM;
        RAISE ex_ExitMain;
  END;
...
...
...
END Usp_Archive_Stop;
/

I haven't included the full procedure script as it is far too long.

Toad gives the following error when the code is loaded into a SQL Editor i.e. it hasn't been run yet:

ERROR line 860, col 7, ending_line 860, ending_col 12, Found 'SELECT', Expecting:  @   or   (   OVER  or   %    .    [   or  (+)  AT  DAY  YEAR  or  **  MULTISET  or   *    /   MOD  REM  or   +    -   ||  or  SAVE  or  DELETE  EXECUTE  INSERT  MERGE  UPDATE

Error Line 860 refers to the following:
      SELECT wave_id

As far as I'm aware the FORALL statement can only be used with a following INSERT, UPDATE or DELETE and not a SELECT statement.  Is this correct?

Can someone explain (in general terms) what the FORALL.... SELECT followed by the DELETE statement are doing?  It is my understanding that only the statement following FORALL (usually INSERT, UPDATE or DELETE) as acting on the collection identified by the FORALL statement, therefore the DELETE statement is completely separate.  If I'm correct, this suggests that the DELETE statement is only acting on the last entry in the collection, not every entry.

I am told by the person supplying this code that it must be correct as it is being used elsewhere!  I would be interested to know what the Oracle experts say about this code and how it would execute.

Strangely, when the code is run it successfully creates the stored procedure even though Toad reports a syntax error.
Start your free trial to view this solution
Question Stats
Zone: Database
Question Asked By: Milleniumaire
Solution Provided By: sdstuber
Participating Experts: 3
Solution Grade: A
Views: 211
Translate:
Loading Advertisement...
02.26.2008 at 06:41AM PST, ID: 20984642

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.26.2008 at 06:42AM PST, ID: 20984654

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.26.2008 at 07:58AM PST, ID: 20985479

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.26.2008 at 08:02AM PST, ID: 20985554

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.26.2008 at 08:07AM PST, ID: 20985600

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.26.2008 at 08:08AM PST, ID: 20985611

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.26.2008 at 08:09AM PST, ID: 20985625

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.26.2008 at 08:15AM PST, ID: 20985677

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.26.2008 at 08:27AM PST, ID: 20985808

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
02.26.2008 at 06:41AM PST, ID: 20984642

Rank: Guru

>> CREATE OR REPLACE PROCEDURE DBO.Usp_Archive_Stop

Are you compiling this in the DBO schema ??
 
02.26.2008 at 06:42AM PST, ID: 20984654
 
02.26.2008 at 07:58AM PST, ID: 20985479

Rank: Master

Sounds like the problem is with Toad... does the version you are using support 10gR2?  I'm pretty sure if you are using a version newer than 8.5, you should be fine...
 
02.26.2008 at 08:02AM PST, ID: 20985554

Rank: Wizard

The procedure is not correct and there is no way this exact code would run anywhere else.


FORALL can only be used with INSERT, UPDATE and DELETE statements.

The purpose of it is when you have a collection (nested table, varray, associative array) and you want to performa the same operation on a bunch of members of that collection then use FORALL which will optimize the execution by binding the collection rather than issuing the same statement a bunch of times for one element at a time.

But it doesn't apply to SELECT statements.

The delete statment doesn't make sense because the "j" index of the forall statement does not apply beyond the forall itself.  So, even FORALL did work with SELECT statements, the Delete would be outside the scope of the forall (it's not really a loop, it just looks like one) so the DELETE on element "j" doesn't apply.


Accepted Solution
 
02.26.2008 at 08:07AM PST, ID: 20985600
Yes, I'm on the latest version of Toad (9.5.0.31) which does support 10g.

Thanks sdstuber, that was exactly the answer I was looking for.  What you say is exactly what I thought was the case and I've checked all Oracle manuals (9i, 10g and 11g) for the syntax the FORALL statement, none of which suggest that it can be used with SELECT statements.

It is very strange that Oracle allows this procedure to compile.  I've no idea how it will run, but one would have thought it would error when reaching this statement, but then maybe not if it compiles okay!

Now I'm feeling a little more sure of myself I will confront the application vendors a little more forcefully and tell them their code is wrong.
 
02.26.2008 at 08:08AM PST, ID: 20985611

Rank: Guru

I agree with sdstuber. Try this.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
...
BEGIN
    -- Delete AFO Wave Detail .
    FOR j IN v_tblOrderNumber.FIRST .. v_tblOrderNumber.LAST LOOP
      SELECT wave_id
        INTO v_vchWaveId
        FROM t_afo_wave_detail
       WHERE wh_id = in_vchWhId
         AND order_number = v_tblOrderNumber(j)
         AND load_seq = in_vchStopId;
 
      DELETE
        FROM t_afo_wave_detail
       WHERE wh_id = in_vchWhId
         AND order_number = v_tblOrderNumber(j)
         AND load_seq = in_vchStopId;
 
    EXCEPTION
      WHEN OTHERS THEN
        out_vchErrorNbr := '-20036';
        out_vchErrorMsg := SQLCODE||' - '||SQLERRM;
        RAISE ex_ExitMain;
  END;
...
Open in New Window
 
02.26.2008 at 08:09AM PST, ID: 20985625

Rank: Guru

Oops, missed the END LOOP; statement to be placed just before the EXCEPTION statement.
 
02.26.2008 at 08:15AM PST, ID: 20985677

Rank: Wizard

jinesh,

that code won't run either.  you're not ending the for loop, plus, it's a subset of code, who knows what else should be in or should not be in the sample block the author provided.
and, v_vchWaveId would get overwritten without use on each loop which probably isn't correct.  Actually, that same problem would exist in the original code too if FORALL worked with select.
 
but, I wouldn't even try to rewrite this.  Whomever you vendor is definitely needs to rewrite it.  You're paying for their product and support, the least they can do is provide syntactically correct code.
 
02.26.2008 at 08:27AM PST, ID: 20985808
Interestingly, I've just cobled together a PL/SQL block to test the use of FORALL...SELECT and the code runs but gives a ORA-10403: no data found error.  I would have expected something more substantial than this as it is clearly not a valid statement.  At least this explains why the procedure compiles.
 
 
02.26.2008 at 08:29AM PST, ID: 20985831
I don't think a SELECT following a FORALL is incorrect syntax as such, which is why it wont throw any errors at compile-time, though it would not work the way we expect it to.

sdstuber: Is it possible to populate a PL/SQL table using the FORALL ?
 
 
02.26.2008 at 08:36AM PST, ID: 20985926
Hi Jinesh, if you don't think FORALL... SELECT is incorrect then please show me the documentation that shows it is a valid syntax.  Every manual I have found describes FORALL as being used with INSERT, UPDATE and DELETE with no mention of SELECT.

I want to get my facts right before I go back to the application vendor, so if this is valid syntax I need to see proof, but I suspect it isn't and is more of a quirk of Oracle that it doesn't raise a compile error, rather than the syntax being correct.
 
 
02.26.2008 at 10:00AM PST, ID: 20986893
@Milleniumaire: You are absolutely correct in stating that FORALL is almost laways used with INSERT, UPDATE and DELETE and not with SELECT.

By my statement - "I don't think a SELECT following a FORALL is incorrect syntax as such" - what I meant is that a PL/SQL program that has a SELECT following the FORALL does not always throw a compile-time error even though the FORALL does not really serve the purpose of the SELECT at all (see below snippet). So while it is correct syntactically (just b'coz it compiles) but not logically or programmatically, a SELECT should NOT used with FORALL and you may confidently approach your vendor for the code correction.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
SQL> ed
Wrote file afiedt.buf
 
  1  DECLARE
  2  TYPE tab_OrderNumber IS TABLE OF dual.dummy%TYPE INDEX BY PLS_INTEGER;
  3  v_tblOrderNumber tab_OrderNumber;
  4  d dual.dummy%TYPE;
  5  i PLS_INTEGER;
  6  BEGIN
  7  FORALL i IN 1 .. v_tblOrderNumber.COUNT
  8         SELECT dummy INTO d FROM dual WHERE dummy = v_tblOrderNumber(i);
  9* END;
SQL> /
 
PL/SQL procedure successfully completed.
 
SQL> 
Open in New Window
 
 
02.26.2008 at 10:59AM PST, ID: 20987448
it will "sort" of run with SELECT in a FORALL but it doesn't make much sense...

you can only select into a scalar which will only work if your FORALL iterates over 0 or 1 rows.

page 11-19 of the pl/sql user guide says you can't use select bulk collect in a forall.

so, to answer jinesh's question...
no, you can't use forall to populate a pl/sql table.

if however, you want to do that there's no reason to use forall, just use a normal select bulk collect into without the forall
 
 
02.26.2008 at 12:44PM PST, ID: 20988573
@sdstuber: Yes, I concur with you. On the FORALL page also, I saw related links to use BULK COLLECT which is kind of similar to the "FORALL" for a SELECT ;)
 
 
 
20080236-EE-VQP-29 / EE_QW_EXPERT_20070906