Showing posts with label Oracle. Show all posts
Showing posts with label Oracle. Show all posts

Wednesday, September 10, 2008

How to use cursor

Implicit cursors - For SQL queries returning single row PL/SQL declares implicit cursors.
Explicit Cursors - Explicit cursors are used in queries that return multiple rows.

Cursor declaration:
without parameters-
CURSOR cursor_name
IS
    SELECT_statement;
    
with parameters-
CURSOR cursor_name (parameter_list)
IS
   SELECT_statement;
   
Open, fetch and close cursor:
Syntax for open - OPEN ;        
Syntax for close - CLOSE ;
Syntax for fetch - FETCH INTO ;

Cursor (explicit) attributes:
%NOTFOUND: Boolean, which evaluates to true, if the last fetch failed.
%FOUND: Boolean, which evaluates to true if the last fetch, succeeded. 
%ROWCOUNT: numeric, which returns number of rows fetched by the cursor so far. 
%ISOPEN: Boolean, which evaluates to true if the cursor is opened otherwise to false.

Loop for cursor:
 - Cursor For Loop itself opens a cursor, read records then closes the cursor automatically. Hence OPEN, FETCH and CLOSE statements are not necessary in it. 

Cursor updattion/deletion:
Steps:
  1. delcare cursor with 'FOR UPDATE' clause
  2. Add 'WHERE CURRENT OF ' in update or delete statement

Example of cursor usage:
1. Open and loop through
OPEN cursor_name FOR
            SELECT * FROM table_name;
         LOOP
            FETCH INTO variables;
         END LOOP;
CLOSE cursor_name;



---
Resource:
1. http://www.exforsys.com/tutorials/oracle-9i/oracle-cursors.html
2. http://www.techonthenet.com/oracle/cursors/declare.php
---

Thursday, July 31, 2008

Oracle error List and solutions

This post includes the Oracle errors i got during my development (java code, Stored Procedure, executing SQL from TOAD/SQLPlus). I tried to give little history for each error to understand the problem I had.


Error:
ORA-01460: unimplemented or unreasonable conversion requested

History:
Environment- Red Hat LInux 4.5/JBoss 4.0.5 GA/Oracle 10g (OS/Application server/database)
I was using ojdbc14.jar for Oracle9i driver and when i try to save a file as BLOB in oracle database from my java code (using JNDI), this exception was thrown.

Solution:

Use ojdbc14.jar of version
10.2.0.4.

Error:
ORA-17009: Closed Statement : Next

History:
In DB Accessor class, first method calls second method which returns a ResultSet and the second method is actually creates DB connection, statement and executes the query. The second method closes the connection and statement in finally block. So, when first method try to traverse the ResultSet


Solution:

Use ojdbc14.jar of version
10.2.0.4.