PLSQL Viewing output of sys_refcursor - plsql

I'm using TOAD for Oracle 11 and am pretty new to SQL. I have written a proc and am now trying to test and view its output. I have written the following block:
DECLARE
cur_test SYS_REFCURSOR;
type t_row is record(psh_code varchar2(20) , pattr_end_date varchar2(20), pperf_gross varchar2(20));
r_test t_row;
BEGIN
procPerfTR(xxx-xxxx', 'xxxxxxx', 'xxxxxxx', 'xxxxxx', :cur_test);
LOOP
FETCH cur_test INTO r_test;
EXIT WHEN cur_test%NOTFOUND;
END LOOP;
CLOSE cur_test;
END;
/
However, I get the following error on the LOOP line
ORA-01001: invalid cursor
The error is on line 10 which is the line that has "LOOP" on it
My proc looks like this
CREATE OR REPLACE PROCEDURE procPerfTR
(
paramPortfCode VARCHAR2,
paramEndDate VARCHAR2,
paramShare VARCHAR2,
paramFreq VARCHAR2,
O_cursorPerf out SYS_REFCURSOR
)
IS
I_cursorPerf SYS_REFCURSOR;
BEGIN
OPEN I_cursorPerf FOR
SELECT PS.PSH_CODE, PP.PATTR_END_DATE, PP.PPERF_GROSS
FROM
PORTFOLIO_PERFORMANCES PP
INNER JOIN PORTF_SHARE PS ON PS.PORTF_SHARE_ID = PP.PORTF_SHARE_ID
INNER JOIN PORTFOLIO P ON P.PORTF_ID = PS.PORTF_ID
INNER JOIN T_FREQUENCY TF ON TF.FREQUENCY_ID = PP.FREQUENCY_ID
WHERE
P.PORTF_CODE = paramPortfCode
AND PP.PATTR_CALCUL_DATE = PP.PATTR_END_DATE
AND PP.PATTR_END_DATE = paramEndDate
AND TF.EXT_CODE = paramFreq
AND PS.PSH_CODE LIKE
(CASE
WHEN paramShare = 'xxxx' THEN '%xxx'
WHEN paramShare = 'xxxx' THEN '%xxx'
END);
O_cursorPerf:=I_cursorPerf;
END;
/

In your proc invocation, don't put a colon in front of the name of the cursor. It should look like
procPerfTR('xxx-xxxx', 'xxxxxxx', 'xxxxxxx', 'xxxxxx', cur_test);
(Note also that I added a single-quote at the start of the first parameter, the absence of which I took to be a simple typo).
Share and enjoy.

Related

Stored Procedure not reading from variable in IF-ELSE statement

I started writing this stored procedure and I faced some issues when I try to pass a variable in my conditional statement.
I can use the parameter BANKN which works fine, but when never I passed the declared variable AC_t somehow the PL/SQL ignores it.
Any idea please what I'm missing here?
create or replace PROCEDURE TTEST1 (
CR IN VARCHAR2,
BANKN IN VARCHAR2,
P_CURSOR OUT SYS_REFCURSOR
)
AS
G_AC CHAR(10);
AC_t Billing.Account %Type;
BEGIN
IF BANKN = 'WLNV' AND AC_t = 'Private'
THEN
IF CR IN (
'EUR',
'CZK',
'USD'
)
THEN
OPEN P_CURSOR
FOR
SELECT G_AC AS GL_ACC,
Billing.Account AS ACC_Type
INTO
G_AC,
AC_t
FROM Billing
INNER JOIN invoice ON Billing.ACC_NO = invoice.ACC_NO;
END IF ;
END IF ;
END;
My aim here is to expand this code by using AC_t value from Billing.ACCount and retrieve what ever data that can be 'Private' or 'Public'.
To do this, I need to use case or IF statement, however when I use
Billing.ACCount, I got an error "not allowed in this context", for this reason I use synonym AC_t but this don't read values from Billing table unless I use it in WHERE clause.
ACC_NO
Account
1
Private
2
Public
Extended code:
IF BANKN = 'WLNV' AND AC_t = 'Private'
THEN
...
...
ELSIF IF BANKN = 'WLNV' AND AC_t = 'Public'
THEN
....
...
You cannot use SELECT ... INTO with a cursor and you need to declare a value for the ac_t variable (but since it is a column in the table you may want a WHERE clause in the cursor). Like this:
CREATE PROCEDURE TTEST1 (
p_CR IN VARCHAR2,
p_BANKN IN VARCHAR2,
P_CURSOR OUT SYS_REFCURSOR
)
AS
BEGIN
IF p_BANKN = 'WLNV'
AND p_CR IN ( 'EUR', 'CZK', 'USD' )
THEN
OPEN P_CURSOR FOR
SELECT G_AC AS GL_ACC,
b.account AS ACC_Type
FROM Billing b
INNER JOIN invoice i
ON b.ACC_NO = i.ACC_NO
WHERE b.account = 'Private';
END IF;
END;
/
Which, if you have the sample data:
CREATE TABLE invoice (acc_no, g_AC) AS
SELECT 1, 2 FROM DUAL;
CREATE TABLE billing (acc_no, account) AS
SELECT 1, 'Private' FROM DUAL;
Then you can call the procedure and print the contents of the cursor using:
DECLARE
cur SYS_REFCURSOR;
v_g_ac INVOICE.G_AC%TYPE;
v_ac_t BILLING.ACCOUNT%TYPE;
BEGIN
ttest1('EUR', 'WLNV', cur);
LOOP
FETCH cur INTO v_g_ac, v_ac_t;
EXIT WHEN cur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE( v_g_ac || ', ' || v_ac_t );
END LOOP;
CLOSE cur;
END;
/
Which outputs:
2, Private
db<>fiddle here

Oracle - store large string in CLOB

I need to save a procedure body into a Clob column with a use of variable. String is longer than 4000 characters, so I can't use VarChar2, but with CLOB variable I receive error "ORA-01422: exact fetch returns more than requested number of rows". Same error appears with Varchar2. My PL/SQL block:
DECLARE
txt_procedure CLOB;
BEGIN
SELECT text INTO txt_procedure
FROM all_source
WHERE name = 'My_procedure'
ORDER BY line;
INSERT INTO TABLE1(ID,DATE,CLOB_COLUMN)
VALUES (my_seq.NEXTVAL,'11.10.2018',txt_procedure);
END;
/
How could I insert procedure body into clob column ?
As you will get multiple rows from your query for every line of your source, the following might help:
DECLARE
txt_procedure CLOB;
BEGIN
FOR source_r IN ( SELECT text
FROM all_source
WHERE name = 'My_procedure'
ORDER BY line
)
LOOP
txt_procedure := txt_procedure || chr(10) || source_r.text;
END LOOP;
INSERT INTO TABLE1(ID,DATE,CLOB_COLUMN)
VALUES (my_seq.NEXTVAL,'11.10.2018',txt_procedure);
END;
/
UPDATE
As an alternative, you might also use the DBMS_METADATA package for this:
DECLARE
txt_procedure CLOB;
BEGIN
txt_procedure := DBMS_METADATA.get_ddl(
object_type => 'PROCEDURE',
name => 'My_procedure',
owner => 'YOUR_SCHEMA'
);
INSERT INTO TABLE1(ID,DATE,CLOB_COLUMN)
VALUES (my_seq.NEXTVAL,'11.10.2018',txt_procedure);
END;
/

Calling a stored procedure with CLOB output

I am trying to use the CLOB datatype as the output parameter in my stored procedure because its resultset exceeds the storage capacity of a var datatype.
How do I execute the procedure? Below are the commands I had run to execute.
I tried assigning the resultset to the the CLOB variable using the INTO query as shown in the query.
var cl CLOB;
EXECUTE procedure_name(:cl);
print cl;
How do i declare the binding variable because if you look at the first command, I am first initializing cl as var I am not able to initialize it as CLOB as it is giving out an error.
This is a sample of my procedure. The actual query in the procedure is 700 lines long.
CREATE OR REPLACE PROCEDURE procedure_name (cl OUT CLOB)
IS
BEGIN OPEN cl FOR
SELECT * FROM .....
statement 1
.
.
.
.
.
statement n
INTO cl
FROM
statement 1
.
.
.
statement n
EXCEPTION
WHEN
OTHERS THEN
DECLARE
err_num NUMBER := SQLCODE;
err_msg VARCHAR2(512) := SQLERRM;
error_id_pk NUMBER;
error_dt DATE;
BEGIN
SELECT (REGEXP_REPLACE(CURRENT_TIMESTAMP, '[^0-9]+', ''))INTO error_id_pk FROM DUAL;
SELECT SYSDATE INTO error_dt FROM DUAL;
INSERT INTO ODS_CONTROL.ERROR_DETAILS(ERROR_ID, ERROR_CODE, ERROR_DATE, PROCEDURE_NAME, ERROR_MSG)
VALUES ( error_id_pk,
err_num,
error_dt,
'PRC_FLEXI_CARD',
err_msg
);
END;
END;
Error message:
Error starting at line : 2 in command -
EXECUTE procedure_name( :clb )
Error report -
ORA-06550: line 1, column 7:
PLS-00905: object procedure_name is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
[TL;DR] VAR is a keyword for declaring a variable and is not a data type; your actual error is due to using invalid syntax when you tried to define your procedure and it has not compiled.
VAR is short for VARIABLE and defines a PL/SQL bind variable.
This declaration has the syntax:
VAR[IABLE] [variable [type] ]
where type represents one of the following:
NUMBER
CHAR
CHAR (n [CHAR | BYTE])
NCHAR
NCHAR (n)
VARCHAR2 (n [CHAR | BYTE])
NVARCHAR2 (n)
BLOB
BFILE
CLOB
NCLOB
REFCURSOR
BINARY_FLOAT
BINARY_DOUBLE
So with:
var cl CLOB;
you are declaring a variable using the VAR keyword and the variable is named cl and has the type CLOB.
Also, your CREATE PROCEDURE statement has a syntax error as you cannot have the single quotes around the procedure name. For example:
CREATE PROCEDURE procedure_name (clb OUT CLOB)
IS
BEGIN
clb := 'test';
END;
/
Then:
VAR cl CLOB;
EXECUTE procedure_name( :cl );
PRINT cl;
Outputs:
test
Updated:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE ERROR_DETAILS(
ERROR_ID NUMBER,
ERROR_CODE NUMBER,
ERROR_DATE DATE,
PROCEDURE_NAME VARCHAR2(30),
ERROR_MSG VARCHAR2(512)
)
/
CREATE PROCEDURE procedure_name (cl OUT CLOB)
IS
BEGIN
SELECT DUMMY
INTO cl
FROM dual
WHERE ROWNUM = 1;
EXCEPTION
WHEN
OTHERS THEN
DECLARE
err_num NUMBER := SQLCODE;
err_msg VARCHAR2(512) := SQLERRM;
BEGIN
INSERT INTO /* ODS_CONTROL. */ ERROR_DETAILS(
ERROR_ID,
ERROR_CODE,
ERROR_DATE,
PROCEDURE_NAME,
ERROR_MSG
) VALUES (
TO_NUMBER( TO_CHAR( CURRENT_TIMESTAMP, 'YYYYMMDDHH24MISSFF9' ) ),
err_num,
SYSDATE,
'PRC_FLEXI_CARD',
err_msg
);
END;
END;
/
Query 1:
SELECT * FROM USER_ERRORS
Results:
No rows selected

How to use an array in PL/SQL?

How do I make arrays in PL / SQL?
I have a string that I want to split on spaces and then loop through them all.
Declare your array like this:
"your array" apex_application_global.vc_arr2;
"your array" := APEX_UTIL.STRING_TO_TABLE("your string",' ');
FOR i IN 1.. "your array".COUNT LOOP
"Your string" := "Your string"|| "your array"(i);
END LOOP;
and there you have it
And this is the ultimate and universal solution without using any packages, just oracle SQL. See a full featured solution (including a pipelining function) over here: http://www.armbruster-it.org/index.php/12-it/pl-sql/20-string-tokenizer-with-oracle-pl-sql
declare
cursor c_tokenizer(ci_string in varchar2, ci_delimiter in varchar2) is
SELECT regexp_substr(str, '[^' || ci_delimiter || ']+', 1, LEVEL) AS splitted_element,
LEVEL AS element_no
FROM (SELECT rownum AS id, ci_string str FROM dual)
CONNECT BY instr(str, ci_delimiter, 1, LEVEL - 1) > 0
AND id = PRIOR id
AND PRIOR dbms_random.value IS NOT null;
l_string varchar2(100) := 'Hello World, I like PL/SQL';
l_delimiter varchar2(1) := ' ';
begin
-- extract each word of the string above (delimited by blank)
for c1 in c_tokenizer(l_string, l_delimiter) loop
dbms_output.put_line(c1.splitted_element);
end loop;
end;
The Result is:
Hello
World,
I
like
PL/SQL

pl sql set variable in procedure

I am not sure how to set variables in a stored procedure which calls another stored procedure. I want to save the value returned and use it later in another select.
I want to do something like this:
PROCEDURE procName(bcur OUT IssCur)
IS
cur2 IssCur;
extCur IssCur;
exlineno varchar2(30);
BEGIN
exlineno :=getExternlineno(exlineno,'50036648','00060');
open cur2 for SELECT concat(SUBSTR(susr2, 0, INSTR(susr2, '-')-1),'' ) from OrderDetail;
the stored procedure to call
PROCEDURE getExternlineno(
oRetValue OUT varchar2,
pKey IN varchar2,
poNum IN varchar2)
AS
Begin
select externlineno into oRetValue from podetail where pokey = pKey and polinenumber = poNum;
end getExternlineno;
once I figure out how to do that then I can also break up something like this(which doesn't understand the proc name:
SELECT concat(concat(SUBSTR(susr2, 0, INSTR(susr2, '-')-1),'' ),' - ' || getExternlineno(exlineno,'50036648','00060')) from OrderDetail;
Egor is right in his comment. You should declare getExternlineno as a function in order to use it inside an SQL query.
The function getExternlineno would then become:
FUNCTION getExternlineno(
pKey IN varchar2,
poNum IN varchar2)
RETURN VARCHAR
AS
DECLARE
oRetValue VARCHAR2(2000); -- Change the precision as per program's requirements.
Begin
select externlineno into oRetValue from podetail where pokey = pKey and polinenumber = poNum;
end getExternlineno;
/
Your procName procedure would then become:
PROCEDURE procName(bcur OUT IssCur)
IS
cur2 IssCur;
extCur IssCur;
exlineno varchar2(30);
BEGIN
exlineno := getExternlineno('50036648','00060'); -- Notice the change in number of arguments here.
open cur2 for SELECT concat(SUBSTR(susr2, 0, INSTR(susr2, '-')-1),'' ) from OrderDetail;
END procName;
/
And your SQL query would become:
SELECT concat(concat(SUBSTR(susr2, 0, INSTR(susr2, '-')-1),'' ),' - '
|| getExternlineno('50036648','00060'))
FROM OrderDetail;

Resources