I am trying to access one my Stored Procedure from java code, where the procedure is returing a PL/SQL table(PACKAGE TABLE) type, as it is easy to handle SYS_REFCURSOR in my java code, I am trying to convert the PL/SQL table to SYS_REFCURSOR in my Stored Procedure. After googling I didn't got any appropriate answer for this conversion. Can someone help me out for this conversion logic?
create or replace PROCEDURE TESTPROC(
INPUT1 IN VARCHAR2,
INPUT2 IN VARCHAR2,
P_PRC OUT SYS_REFCURSOR) AS
PACKAGE_TABLE PACKAGE.TESTTABLE;
BEGIN
PACKAGE_TABLE := FUNCTION_RETURN_PACKAGE_TABLE(INPUT1, INPUT2);
-- **LOGIC TO CONVERT PACAKGE_TABLE TO SYS_REFCURSOR GOES HERE**
END TESTPROC;
You can use TABLE operator for this
create or replace PROCEDURE TESTPROC(
INPUT1 IN VARCHAR2,
INPUT2 IN VARCHAR2,
P_PRC OUT SYS_REFCURSOR) AS
BEGIN
OPEN P_PRC FOR
SELECT * FROM TABLE(FUNCTION_RETURN_PACKAGE_TABLE(INPUT1, INPUT2));
END TESTPROC;
But you should keep in mind that you have to have schema level pl\sql table type (for oracle <12c). Also notice that SELECT * FROM brings you one-feild rows with your-plsql-table-row-type value.
Related
I have the following stored procedure:
CREATE OR REPLACE PROCEDURE SP
(
query IN VARCHAR2(200),
CURSOR_ OUT SYS_REFCURSOR
)
AS
row_ PROCESSED_DATA_OBJECT;
processed PROCESSED_DATA_TABLE;
BEGIN
.....
END;
with
CREATE TYPE processed_data_obj AS OBJECT(
id INTEGER,
value FLOAT
);
/
CREATE OR REPLACE TYPE processed_data_table AS TABLE OF processed_data_obj;
/
I call the stored procedure passing the query to be executed as input parameter.
The query is something like that:
SELECT A,B FROM TABLE WHERE
where A,B and TABLE are not fixed (defined at runtime during java program execution), so I don't know their values in advance.
How could I fetch/store each returned row in my structure?
processed PROCESSED_DATA_TABLE;
Thanks
This is one way you can process a dynamically generated query into a user defined type. Note that, in order for this to work, the structure of your query (columns) must match the data type structure of your type (attributes) otherwise you're in for trouble.
CREATE TYPE processed_data_obj AS OBJECT(
ID INTEGER,
VALUE FLOAT,
constructor FUNCTION processed_data_obj RETURN self AS result
);
/
CREATE OR REPLACE TYPE BODY processed_data_obj IS
constructor FUNCTION processed_data_obj RETURN self AS result IS
BEGIN
RETURN;
END;
END;
/
CREATE OR REPLACE TYPE processed_data_table AS TABLE OF processed_data_obj;
/
CREATE OR REPLACE PROCEDURE sp (
p_query IN VARCHAR2
) AS
cursor_ sys_refcursor;
processed processed_data_table := processed_data_table();
BEGIN
OPEN cursor_ FOR p_query;
loop
processed.EXTEND;
processed(processed.count) := processed_data_obj();
fetch cursor_ INTO processed(processed.count).ID, processed(processed.count).VALUE;
exit WHEN cursor_%notfound;
dbms_output.put_line(processed(processed.count).ID||' '||processed(processed.count).VALUE);-- at this point do as you please with your data.
END loop;
CLOSE cursor_; -- always close cursor ;)
processed.TRIM; -- or processed.DELETE(processed.count);
END sp;
I noticed that, originally, you did put CURSOR_ as an output parameter in your stored procedure, if that is still your goal, you can create your procedure as:
CREATE OR REPLACE PROCEDURE sp (
p_query IN VARCHAR2,
cursor_ out sys_refcursor
) AS
processed processed_data_table := processed_data_table();
BEGIN
OPEN cursor_ FOR p_query;
loop
processed.EXTEND;
processed(processed.count) := processed_data_obj();
fetch cursor_ INTO processed(processed.count).ID, processed(processed.count).VALUE;
exit WHEN cursor_%notfound;
dbms_output.put_line(processed(processed.count).ID||' '||processed(processed.count).VALUE);-- at this point do as you please with your data.
END loop;
-- cursor remains open
processed.TRIM; -- or processed.DELETE(processed.count);
END sp;
In this case just be conscious about handling your cursor properly and always close it when you're done with it.
I have two columns in my table(TRANSACTION) in ORACLE which are XMLTYPE(XML_IN and XML_OUT). My procedure is not working because I don't know how to convert them to VARCHAR or something(I just think that this is the error). My procedure is:
PROCEDURE SEARCH_XML
(
P_ID_TRANSACTION IN TRANSACTION.ID_TRANSACTION%TYPE,
P_CURSOR OUT T_CURSOR
)
IS
BEGIN
OPEN P_CURSOR FOR
SELECT T.XML_IN, T.XML_OUT
FROM TRANSACTION T
WHERE T.ID_TRANSACTION = P_ID_TRANSACTION;
END SEARCH_XML;
When I call this procedure error message in VisualStudio2008 is: "Unsupported oracle data type USERDEFINED encountered." Any idea how is this working?
XMLType has two methods: getStringVal() and getClobVal() which will convert the XML structure to their string representations (as a VARCHAR2 and CLOB respectively). Unless you know that your XML output is going to always be less than 4000 characters (bytes) then you will probably want to use getClobVal() like this:
PROCEDURE SEARCH_XML
(
P_ID_TRANSACTION IN TRANSACTION.ID_TRANSACTION%TYPE,
P_CURSOR OUT T_CURSOR
)
IS
BEGIN
OPEN P_CURSOR FOR
SELECT T.XML_IN.getClobVal() AS XML_IN,
T.XML_OUT.getClobVal() AS XML_OUT
FROM TRANSACTION T
WHERE T.ID_TRANSACTION = P_ID_TRANSACTION;
END SEARCH_XML;
I am creating a stored procedure in oracle that is selecting records from login table -
create or replace procedure login_info
(username IN varchar2, password IN varchar2, result OUT number)
as
begin
select * from login;
end;
Whenever I am going to compile this it shows an error:
PLS-00428: an INTO clause is expected in this SELECT statement
What does this mean? I do not understand this.
You have to store the result of your SELECT statement into a variable, you can use sys_refcursor to display the result.
create or replace procedure login_info
(username IN varchar2, password IN varchar2, result OUT number, result_out OUT SYS_REFCURSOR)
as
l_query varchar2(1000) := Null;
begin
l_query := 'select * from login';
open result_out
for l_query;
end;
above code will give you the output
PLS-00428: an INTO clause is expected in this SELECT statement
That means you need an INTO close when you issue bare SELECT from PL/SQL.
:D
More constructively: where do you think the result of your select would go in that code fragment?
create or replace procedure login_info
(username IN varchar2, password IN varchar2, result OUT number)
as
begin
select * from login;
end;
You have to retrieve it somehow in order to be processed by your PL/SQL code. Assuming you have several rows to collect, you should use BULK COLLECT INTO:
create or replace procedure login_info
(username IN varchar2, password IN varchar2, result OUT number)
as
type my_tbl_type IS TABLE OF login%ROWTYPE;
my_tbl my_tbl_type;
begin
select * BULK COLLECT INTO my_tbl from login;
-- do whatever you
-- need here
-- on `my_tbl`.
end;
As a final note, maybe are you looking for an explicit CURSOR instead? You should definitively take a look at PL/SQL 101: Working with Cursors. This is an interesting discussion both about SELECT ... INTO ... and CURSOR manipulation.
I am having difficulty with writing my stored procedure within a package. Below is my stored procedure that compiles fine outside of the package, but I believe needs to be written differently within the package body. I admit this is my first time using a package...
create or replace PROCEDURE SP_COMMENT(P_MEMBER_ID IN VARCHAR2, P_MEMBER_LASTNAME IN
VARCHAR2, P_MEMBER_FIRSTNAME IN VARCHAR2, P_MEMBER_STARTDATE IN DATE,
P_MEMBER_ENDDATE IN DATE, P_PRODUCT_CAT_CODE IN VARCHAR2, P_COMMENT IN VARCHAR2,
COMMENT_CURSOR out sys_refcursor)
AUTHID CURRENT_USER
IS
BEGIN
EXECUTE IMMEDIATE
'INSERT INTO TEST
(
MEMBER_ID,
MEMBER_LASTNAME,
MEMBER_FIRSTNAME,
MEMBER_STARTDATE,
MEMBER_ENDDATE,
PRODUCT_CAT_CODE,
COMMENTS
)
VALUES
(
p_member_id,
p_member_lastname,
p_member_firstname,
p_member_startdate,
p_member_enddate,
p_product_cat_code,
p_comment)';
commit;
open COMMENT_CURSOR for select * from sconti.TEST;
END;
Below is the package that I started, and which is not working:
CREATE OR REPLACE
PACKAGE COMMENT_TEST IS
PROCEDURE SP_COMMENT(P_MEMBER_ID IN VARCHAR2, P_MEMBER_LASTNAME IN VARCHAR2,
P_MEMBER_FIRSTNAME IN VARCHAR2, P_MEMBER_STARTDATE IN DATE,
P_MEMBER_ENDDATE IN DATE, P_PRODUCT_CAT_CODE IN VARCHAR2, P_COMMENT IN VARCHAR2,
COMMENT_CURSOR out sys_refcursor) IS
BEGIN
EXECUTE IMMEDIATE
'INSERT INTO TEST
(
MEMBER_ID,
MEMBER_LASTNAME,
MEMBER_FIRSTNAME,
MEMBER_STARTDATE,
MEMBER_ENDDATE,
PRODUCT_CAT_CODE,
COMMENTS
)
VALUES
(
p_member_id,
p_member_lastname,
p_member_firstname,
p_member_startdate,
p_member_enddate,
p_product_cat_code,
p_comment)';
commit;
open COMMENT_CURSOR for select * from sconti.TEST;
END;
END COMMENT_TEST;
I look forward to any response to assist me....thanks!
I can't say for certain (because you haven't shared the error you're getting), but most basic error is a lack of understanding of the specification/body.
You've put the code into the package specification, rather than the body. The specification should just have the procedure declarations (i.e. no begin and end), where as the body has the full content of the procedure.
While it won't affect compilation, there is another problem: the SQL inside the string can't access the parameters supplied to the procedure. If you must use dynamic SQL (and there's absolutely no reason to in this case), then you need a using clause to bind the variable into the dynamic statement. In addition, making the SQL static will allow the SQL statement to be validated at compile-time, which has obvious advantages.
A revised packaged (specification and body) is below.
CREATE OR REPLACE PACKAGE comment_test IS
PROCEDURE sp_comment (p_member_id IN VARCHAR2,
p_member_lastname IN VARCHAR2,
p_member_firstname IN VARCHAR2,
p_member_startdate IN DATE,
p_member_enddate IN DATE,
p_product_cat_code IN VARCHAR2,
p_comment IN VARCHAR2,
comment_cursor OUT SYS_REFCURSOR);
END comment_test;
/
CREATE OR REPLACE PACKAGE BODY comment_test IS
PROCEDURE sp_comment (p_member_id IN VARCHAR2,
p_member_lastname IN VARCHAR2,
p_member_firstname IN VARCHAR2,
p_member_startdate IN DATE,
p_member_enddate IN DATE,
p_product_cat_code IN VARCHAR2,
p_comment IN VARCHAR2,
comment_cursor OUT SYS_REFCURSOR) IS
BEGIN
INSERT INTO test (member_id,
member_lastname,
member_firstname,
member_startdate,
member_enddate,
product_cat_code,
comments)
VALUES (p_member_id,
p_member_lastname,
p_member_firstname,
p_member_startdate,
p_member_enddate,
p_product_cat_code,
p_comment);
COMMIT;
OPEN comment_cursor FOR SELECT * FROM sconti.test;
END;
END comment_test;
/
I created a procedure with dynamic sql,but cannot run it successfully.
create or replace procedure getdata(string par1, results out cursor)
as
declare sqlBase varchar2(100);
begin
sqlBase := 'open '||results|| ' for select * from studetns';
end;
After running, the following error message pops up:
PLS-00306, wrong number or types of arguments in call to '||'
I just need to filter data by some parameters ,but some parameters may be null or empty,
so I need to filter dynamic. like if(par1 is not null) then ........
so here I need to use dynamic sql. in C# programe, use cursor to return result.
like here ,I use cursor type to open select statements.
but in sql editor, I get right sql statement.
Can Somebody help me with this?
Your syntax is a little bit wrong. Try with this:
create or replace procedure getdata(par1 varchar2, par2 varchar2, results out sys_refcursor)
as
begin
open results for
select *
from students
where name = nvl(par1, name)
and surname = nvl(par2, surname);
end;
Why do you need parameter par1? Better to use PL/SQL type varchar2, not string. They work the same, but varchar2 is a base data type, while string is a subtype of it.
Depending on what you want to achieve, I would try something like that:
create or replace procedure getdata(par1 varchar2, results out sys_refcursor)
as
sqlBase varchar2(100);
begin
sqlBase := 'begin open :X for select * from students;end;';
execute immediate sqlbase USING IN OUT results;
end;
You can't concatenate a cursor into a string as you tried it, that's where your error came from.
But if you could clearify your question we could help you better.