Home » Infrastructure » Windows » Running Execute Command from C# to return a SYS_REFCURSOR! (Oracle 11g Release 2)
|
|
Re: Running Execute Command from C# to return a SYS_REFCURSOR! [message #502443 is a reply to message #502441] |
Wed, 06 April 2011 06:06   |
 |
aucrun
Messages: 114 Registered: February 2011
|
Senior Member |
|
|
Sorry,
My mistake ... the correct code that I have is bellow:
ExecuteSQL = "DECLARE v sys_refcursor; \n";
ExecuteSQL += "BEGIN \n";
ExecuteSQL += "execute :v calculateparentclassnodes_del3('77014000','GPC'); \n";
ExecuteSQL += "END; \n";
Is there any way of simply put something like this:
string ExecuteSQL= "Execute calculateparentclassnodes_del3('77014000','GPC');";
OracleCommand cmd = new OracleCommand(ExecuteSQL, con);
OracleDataAdapter da = new OracleDataAdapter(cmd);
[Updated on: Wed, 06 April 2011 06:17] Report message to a moderator
|
|
|
Re: Running Execute Command from C# to return a SYS_REFCURSOR! [message #502448 is a reply to message #502443] |
Wed, 06 April 2011 06:53   |
 |
Michel Cadot
Messages: 68757 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Quote:ExecuteSQL = "DECLARE v sys_refcursor; \n";
This part is useless you don't use it in the PL/SQL block.
Quote:ExecuteSQL += "execute :v calculateparentclassnodes_del3('77014000','GPC'); \n";
This should be
ExecuteSQL += " :v := calculateparentclassnodes_del3('77014000','GPC'); \n";
or
ExecuteSQL += " :1 := calculateparentclassnodes_del3('77014000','GPC'); \n";
or
ExecuteSQL += " ? := calculateparentclassnodes_del3('77014000','GPC'); \n";
depending on the way you reference bind variables in C# (which I don't know).
Then you have to call the procedure that makes the bind between your C# variable and the bind variable inside the PL/SQL block.
By the way, also remove the "\n" I'm not sure how it will be interpreted at the end.
Regards
Michel
[Updated on: Wed, 06 April 2011 06:55] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
Re: Running Execute Command from C# to return a SYS_REFCURSOR! [message #502467 is a reply to message #502466] |
Wed, 06 April 2011 09:09   |
 |
aucrun
Messages: 114 Registered: February 2011
|
Senior Member |
|
|
Because of company policies ....
Thanks any way!
I've tried to manage myself with a workaround that includes the usage of an Package like that:
create or replace
PACKAGE Pkg_parentclasscatycode IS
TYPE classparent_tbl IS TABLE OF classrelation.parentclasscategorycode%TYPE INDEX BY BINARY_INTEGER;
FUNCTION calculateparentclassnodes_Cur ( iCode IN VARCHAR2, iAgency IN VARCHAR2) return classparent_tbl;
END Pkg_parentclasscatycode;
create or replace
PACKAGE BODY Pkg_parentclasscatycode AS
FUNCTION calculateparentclassnodes_Cur(
iCode IN VARCHAR2,
iAgency IN VARCHAR2)
RETURN classparent_tbl
AS
CURSOR cur_parentclasscatycode (in_iCode IN VARCHAR2, in_iAgency IN VARCHAR2 ) IS
WITH parents (parentclasscategorycode ) AS (SELECT parentclasscategorycode FROM classrelation WHERE childclasscategorycode=in_iCode
and classagencyname=in_iAgency
UNION ALL
SELECT cl.parentclasscategorycode FROM classrelation cl, parents pa WHERE cl.childclasscategorycode = pa.parentclasscategorycode
AND cl.classagencyname = in_iAgency)
SELECT distinct parentclasscategorycode FROM parents;
parentClassNodeCode_TabAux classparent_tbl;
BEGIN
IF NOT cur_parentclasscatycode%ISOPEN then
OPEN cur_parentclasscatycode(iCode, iAgency);
END IF;
FETCH cur_parentclasscatycode bulk collect into parentClassNodeCode_TabAux;
return parentClassNodeCode_TabAux;
END calculateparentclassnodes_Cur;
END Pkg_parentclasscatycode;
But when I execute the next statement:
select Pkg_parentclasscatycode.calculateparentclassnodes_Cur('77014000','GPC') from dual
it gives me allways the necxt error:
ORA-00902: tipo de dados inválido
00902. 00000 - "invalid datatype"
*Cause:
*Action:
Error at Line: 2 Column: 7
I just want to return a structure with the results of that Cursor in an only commandtext .
[Updated on: Wed, 06 April 2011 09:14] Report message to a moderator
|
|
|
|
Re: Running Execute Command from C# to return a SYS_REFCURSOR! [message #502510 is a reply to message #502475] |
Wed, 06 April 2011 21:20   |
 |
Barbara Boehmer
Messages: 9105 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
If you use a sql type instead of a pl/sql type, then you can select it from sql and use the table function, as shown below.
SCOTT@orcl_11gR2> create table classrelation
2 (parentclasscategorycode varchar2 (23),
3 childclasscategorycode varchar2 (22),
4 classagencyname varchar2 (15))
5 /
Table created.
SCOTT@orcl_11gR2> insert into classrelation values
2 ('77000000', '77010000', 'GPC')
3 /
1 row created.
SCOTT@orcl_11gR2> insert into classrelation values
2 ('77010000', '77014000', 'GPC')
3 /
1 row created.
SCOTT@orcl_11gR2> insert into classrelation values
2 ('77014000', '2', 'GPC')
3 /
1 row created.
SCOTT@orcl_11gR2> create or replace type classparent_tbl as table of varchar2 (23);
2 /
Type created.
SCOTT@orcl_11gR2> create or replace
2 PACKAGE Pkg_parentclasscatycode IS
3 -- TYPE classparent_tbl IS TABLE OF classrelation.parentclasscategorycode%TYPE INDEX BY BINARY_INTEGER;
4 FUNCTION calculateparentclassnodes_Cur ( iCode IN VARCHAR2, iAgency IN VARCHAR2) return classparent_tbl;
5 END Pkg_parentclasscatycode;
6 /
Package created.
SCOTT@orcl_11gR2> show errors
No errors.
SCOTT@orcl_11gR2> create or replace
2 PACKAGE BODY Pkg_parentclasscatycode AS
3 FUNCTION calculateparentclassnodes_Cur(
4 iCode IN VARCHAR2,
5 iAgency IN VARCHAR2)
6 RETURN classparent_tbl
7 AS
8 CURSOR cur_parentclasscatycode (in_iCode IN VARCHAR2, in_iAgency IN VARCHAR2 ) IS
9 WITH parents (parentclasscategorycode ) AS (SELECT parentclasscategorycode FROM classrelation WHERE childclasscategorycode=in_iCode
10 and classagencyname=in_iAgency
11 UNION ALL
12 SELECT cl.parentclasscategorycode FROM classrelation cl, parents pa WHERE cl.childclasscategorycode = pa.parentclasscategorycode
13 AND cl.classagencyname = in_iAgency)
14 SELECT distinct parentclasscategorycode FROM parents;
15
16 parentClassNodeCode_TabAux classparent_tbl;
17
18 BEGIN
19 IF NOT cur_parentclasscatycode%ISOPEN then
20 OPEN cur_parentclasscatycode(iCode, iAgency);
21 END IF;
22
23 FETCH cur_parentclasscatycode bulk collect into parentClassNodeCode_TabAux;
24 return parentClassNodeCode_TabAux;
25 END calculateparentclassnodes_Cur;
26 END Pkg_parentclasscatycode;
27 /
Package body created.
SCOTT@orcl_11gR2> show errors
No errors.
SCOTT@orcl_11gR2> select Pkg_parentclasscatycode.calculateparentclassnodes_Cur('77014000','GPC')
2 from dual
3 /
PKG_PARENTCLASSCATYCODE.CALCULATEPARENTCLASSNODES_CUR('77014000','GPC')
--------------------------------------------------------------------------------
CLASSPARENT_TBL('77010000', '77000000')
1 row selected.
SCOTT@orcl_11gR2> select *
2 from table
3 (Pkg_parentclasscatycode.calculateparentclassnodes_Cur
4 ('77014000','GPC'))
5 /
COLUMN_VALUE
-----------------------
77010000
77000000
2 rows selected.
SCOTT@orcl_11gR2>
|
|
|
Re: Running Execute Command from C# to return a SYS_REFCURSOR! [message #502560 is a reply to message #502510] |
Thu, 07 April 2011 05:40   |
 |
aucrun
Messages: 114 Registered: February 2011
|
Senior Member |
|
|
Michel Cadot and Barbara Boehmer I want to say both of you a huge THANK YOU.
This final solution works just fine!
One more question, if I may ...
What is more efficient ...
This:
CURSOR cur_parentclasscatycode (in_iCode IN VARCHAR2, in_iAgency IN VARCHAR2 ) IS
WITH parents (parentclasscategorycode ) AS (SELECT parentclasscategorycode FROM classrelation WHERE childclasscategorycode=in_iCode
and classagencyname=in_iAgency
UNION ALL
SELECT cl.parentclasscategorycode FROM classrelation cl, parents pa WHERE cl.childclasscategorycode = pa.parentclasscategorycode
AND cl.classagencyname = in_iAgency)
SELECT distinct parentclasscategorycode FROM parents;
or this:
OPEN cur_testes FOR select parentclasscategorycode
from
classrelation
where
classagencyname=iAgency
start with
childclasscategorycode = iCode
connect by
prior parentclasscategorycode = childclasscategorycode;
Thanks!
[Updated on: Thu, 07 April 2011 05:41] Report message to a moderator
|
|
|
|
|
Goto Forum:
Current Time: Sat May 03 21:52:15 CDT 2025
|