Home » SQL & PL/SQL » SQL & PL/SQL » newbie problems
newbie problems [message #7690] Tue, 01 July 2003 07:08 Go to next message
Fil
Messages: 7
Registered: July 2003
Junior Member
Hi, I'm new to Oracle and I am getting more than a problem.
Hope someone can help me...at least in one of my troubles...

1) recordset

I'm used to working on ASP pages that get a recorset from SQL Server stored procedure. I searched this forum and the Web for answer about "how do I get a RS?". Now I know more than one way to do it, but a new question comes: why just VB programmers ask for this? Are there any better ways to get multiple records from Oracle? I'm working with JSP pages that get data from Oracle through EJB in a Weblogic environment..

Also: EJB gives me (to the JSP pages) data in a hashtable, and that way I loose the "order by" of the query. Any suggestion about how to mantain the right order?

2) I'm not able to run an oracle procedure from SQL*PLUS. I swear I search the Web for the syntax, but couldn't find it :(
In SQL Server query Analizer was EXEC PROCEDURE_NAME (par1, par2);
I think in SQL*Plus it should be CALL PROCEDURE_NAME (par1, par2)...for instance CALL SELECT_USER_BY_PK (1);

...but it doesn't work

3) other problems...

why this proc doesn't work?

CREATE OR REPLACE PROCEDURE userschema.ProcName (
list_id IN VARCHAR2 DEFAULT NULL,
p_recordset OUT Types.cursor_type)
AS
list_id_ VARCHAR2(8000) := list_id;
v_sql varchar2(4000);
StoO_selcnt INTEGER;
StoO_error INTEGER;
StoO_rowcnt INTEGER;
StoO_crowcnt INTEGER := 0;
StoO_fetchstatus INTEGER := 0;
StoO_errmsg VARCHAR2(255);
StoO_sqlstatus INTEGER;
-- 1,2,3,4,5,6,7,8

BEGIN
v_sql varchar2(4000) := 'select * from userschema.TableName where pk IN ( ' || list_id_ || ' )';
OPEN p_recordset FOR v_sql;


RETURN;

END ProcName;

============
Re: newbie problems [message #7696 is a reply to message #7690] Tue, 01 July 2003 12:46 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
1) Your example code that uses a REF CURSOR to return a result set is the best way to return multiple rows from a procedure.

2)
exec procname(p1, ..., pn)
 
or
 
begin
  procname(p1, ..., pn);
end;
/


The first option (exec) is really just a wrapper for the second.

3) First off, at least with your example code, you do not need to use dynamic SQL. A variable IN list is best handled with an object type. I do this all the time - it works great. This is a very common question.

sql>create or replace type myNumberTable is table of number;
  2  /
 
Type created.
 
sql>create or replace function f_get_number_table_from_string(
  2    p_string     in  varchar2,
  3    p_delimiter  in  varchar2 default ',')
  4    return mynumbertable
  5  is
  6    v_string  long := p_string || p_delimiter;
  7    v_pos     pls_integer;
  8    v_data    mynumbertable := mynumbertable();
  9  begin
 10    loop
 11      v_pos := instr(v_string, p_delimiter);
 12      exit when (nvl(v_pos, 0) = 0);
 13      v_data.extend;
 14      v_data(v_data.count) := trim(substr(v_string, 1, v_pos - 1));
 15      v_string := substr(v_string, v_pos + 1);
 16    end loop;
 17    return (v_data);
 18  end f_get_number_table_from_string;
 19  /
 
Function created.
 
sql>create or replace procedure myproc( 
  2    p_list_id in varchar2 default null,
  3    p_recordset out sys_refcursor)  -- this is 9i only, your cursor type is fine in 8i
  4  is
  5  begin
  6    open p_recordset for 
  7      select object_id, object_name
  8        from user_objects
  9        where object_id in (select *
 10                              from table(cast(f_get_number_table_from_string(p_list_id) as myNumb
erTable)));
 11  end;
 12  /
 
Procedure created.
 
sql>var rc refcursor
sql>exec myproc('30136,31510,30140', :rc)
 
PL/SQL procedure successfully completed.
 
sql>print rc
 
OBJECT_ID
---------
OBJECT_NAME
------------------------------------------------------------------------------
    30136
MYNUMBERTABLE
    30140
F_GET_NUMBER_TABLE_FROM_STRING
    31510
MYPROC
 
3 rows selected.
Re: newbie problems [message #7706 is a reply to message #7696] Wed, 02 July 2003 03:29 Go to previous message
Fil
Messages: 7
Registered: July 2003
Junior Member
Thank you Todd very very much.
You solved more than one (...ops, more than 3) problems! :-)

Thanks
Fil
Previous Topic: Custom Object
Next Topic: altering column places in the table
Goto Forum:
  


Current Time: Thu Apr 25 18:40:49 CDT 2024