no_data_found exception in oracle pl/sql while using collection - oracle

DECLARE
TYPE EmpList IS TABLE OF varchar2(50) INDEX BY BINARY_INTEGER;
temp SYS_REFCURSOR;
v_temp varchar2(50);
v_emp EmpList;
BEGIN
v_emp (1) := 'gaurav';
v_emp (2) := 'manu';
open temp for select v_emp(level) from dual connect by level<=2;
loop
fetch temp into v_temp;
exit when temp%notfound;
DBMS_OUTPUT.put_line (v_temp);
end loop;
close temp;
--the below part works, then why not the above part dint works
for i in v_emp.first..v_emp.last
loop
dbms_output.put_line(v_emp(i));
end loop;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (SQLERRM);
END;
Can anyone please tell me why this collection dint work here??,It is giving me no_data_found exception.

the reason for failure is that
v_emp(level)
is evaluated (as its a variable) at runtime so would actually evaluate on all rows to
v_emp(0);
you could see this if you changed your array to
v_emp (0) := 'gaurav';
v_emp (1) := 'manu';
the proper way (in case you were not aware) is:
create TYPE EmpList IS TABLE OF varchar2(50);
/
and then :
v_emp := EmpList('gaurav', 'manu');
open temp for select column_value from table(v_emp);

Try This:
DECLARE
TYPE EmpList IS TABLE OF varchar2(50) INDEX BY BINARY_INTEGER;
temp SYS_REFCURSOR;
v_temp varchar2(50);
v_emp EmpList;
BEGIN
v_emp (0) := 'gaurav';
v_emp (1) := 'manu';
open temp for select v_emp(level) from dual connect by level<=2;
loop
fetch temp into v_temp;
exit when temp%notfound;
DBMS_OUTPUT.put_line ('v_temp' || v_temp);
end loop;
close temp;
--the below part works, then why not the above part dint works
for i in v_emp.first..v_emp.last
loop
dbms_output.put_line(v_emp(i));
end loop;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (SQLERRM);
END;

Related

Read file and generate query in plsql oracle 11

I am writing a stored procedure where I have to do following: I want to have a file (any format properties, json, xml) which will have information of which columns I want to extract from my table.
For example: my table has columns A,B,C,D,E, and suppose my file.properties has below information
A=1
B=0
C=1
D=1
F=0
So my generated query should be Select A,C,D from my table;
How can I do this in Oracle 11G?
I think you need this
SQL> set serveroutput on;
SQL> create or replace procedure pr_dynamic_sql( v_result out sys_refcursor ) is
v_outfile utl_file.file_type;
v_path varchar2(100) := 'UTL_FILE_DIR';
-- alias for the directory where your text files generated at OS.
v_row varchar2(100);
v_file varchar2(100);
v_letter varchar2(10);
v_number varchar2(10);
v_sql varchar2(100):= 'select ';
begin
v_file := 'myfile.properties';
v_outfile := utl_file.fopen(v_path, v_file, 'r');
loop
begin
utl_file.get_line(v_outfile,v_row);
v_letter := regexp_substr(v_row,'[^=]');
v_number := substr(regexp_substr(v_row,'[^=]+$'),1,1);
if v_number = '1' then
v_sql := v_sql||v_letter||',';
end if;
exception when no_data_found then exit;
end;
end loop;
utl_file.fclose(v_outfile);
v_sql := rtrim(v_sql,',')||' from mytable';
open v_result for v_sql;
end;
and call
SQL> begin
pr_dynamic_sql(v_result => :v_result);
end;
/
to get results as of cursor type.

Dynamic select execution missing expression error

I am using Oracle 12, and I want to make a dynamic procedure which selects rows from specific table but according to an unknown conditio. That condition will be specified as input parameter.
Suppose I have a column called employee id and I want to call the procedure
with the following condition
execute s('employeeid = 2')
My code is
create or replace procedure s (condition varchar)
as
TYPE EmpCurTyp IS REF CURSOR; -- define weak REF CURSOR type
emp_cv EmpCurTyp; -- declare cursor variable
my_ename VARCHAR2(15);
my_sal NUMBER := 2;
mycondition varchar2(100):=condition;
BEGIN
OPEN emp_cv FOR -- open cursor variable
'SELECT employeeid, employeename FROM employees WHERE = :s' USING mycondition;
END;
but I am getting an error
missing expression
What am I doing wrong, and will the result of this procedure be selected rows from employees table that satisfy applied condition ?
The USING is meant to handle values, not pieces of code; if you need to edit your query depending on an input parameter ( and I believe this is a very dangerous way of coding), you should treat the condition as a string to concatenate to the query.
For example, say you have this table:
create table someTable(column1 number)
This procedure does somthing similar to what you need:
create or replace procedure testDyn( condition IN varchar2) is
cur sys_refcursor;
begin
open cur for 'select column1 from sometable where ' || condition;
/* your code */
end;
Hot it works:
SQL> exec testDyn('column1 is null');
PL/SQL procedure successfully completed.
SQL> exec testDyn('column99 is null');
BEGIN testDyn('column99 is null'); END;
*
ERROR at line 1:
ORA-00904: "COLUMN99": invalid identifier
ORA-06512: at "ALEK.TESTDYN", line 4
ORA-06512: at line 1
This is not embedded in a procedure yet but I tested this and works:
DECLARE
TYPE OUT_TYPE IS TABLE OF VARCHAR2 (20)
INDEX BY BINARY_INTEGER;
l_cursor INTEGER;
l_fetched_rows INTEGER;
l_sql_string VARCHAR2 (250);
l_where_clause VARCHAR2 (100);
l_employeeid VARCHAR2 (20);
l_employeename VARCHAR2 (20);
l_result INTEGER;
o_employeeid OUT_TYPE;
o_employeename OUT_TYPE;
BEGIN
l_cursor := DBMS_SQL.OPEN_CURSOR;
l_sql_string := 'SELECT employeeid, employeename FROM employees WHERE ';
l_where_clause := 'employeeid = 2';
l_sql_string := l_sql_string || l_where_clause;
DBMS_SQL.PARSE (l_cursor, l_sql_string, DBMS_SQL.V7);
DBMS_SQL.DEFINE_COLUMN (l_cursor,
1,
l_employeeid,
20);
DBMS_SQL.DEFINE_COLUMN (l_cursor,
2,
l_employeename,
20);
l_fetched_rows := 0;
l_result := DBMS_SQL.EXECUTE_AND_FETCH (l_cursor);
LOOP
EXIT WHEN l_result = 0;
DBMS_SQL.COLUMN_VALUE (l_cursor, 1, l_employeeid);
DBMS_SQL.COLUMN_VALUE (l_cursor, 2, l_employeename);
l_fetched_rows := l_fetched_rows + 1;
o_employeeid (l_fetched_rows) := l_employeeid;
o_employeename (l_fetched_rows) := l_employeename;
l_result := DBMS_SQL.FETCH_ROWS (l_cursor);
END LOOP;
DBMS_SQL.CLOSE_CURSOR (l_cursor);
DBMS_OUTPUT.PUT_LINE (o_employeeid (1));
DBMS_OUTPUT.PUT_LINE (o_employeename (1));
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE ('GENERAL FAILURE: ' || SQLERRM);
END;

Oracle PL/SQL Ref Cursor Function Missing Char

When I run this code, the result should be 636790 but 63679 is only returned. Beating brains out on this one!!! Why the missing digit? Source table and column contain correct number of 636790.
create or replace package jt_types
as
type ttest is ref cursor;
end;
CREATE OR REPLACE FUNCTION jt_test
RETURN jt_types.ttest
IS
o_return jt_types.ttest;
BEGIN
OPEN o_return FOR
SELECT offn_id
FROM jt_offn_4
where offn_ID in (636790)
ORDER BY offn_id;
RETURN o_return;
END jt_test;
DECLARE
l_ids jt_types.ttest;
l_id NUMBER;
BEGIN
l_ids := jt_test;
LOOP
FETCH l_ids INTO l_id;
EXIT WHEN l_ids%NOTFOUND;
dbms_output.put_line('Rec: ' || l_id);
END LOOP;
CLOSE l_ids;
END;
The easiest way to do is to return sys_refcursor, here is the code:
create or replace
function jt_test
return sys_refcursor
is
o_return sys_refcursor;
begin
open o_return for
select offn_id
from jt_offn_4
where offn_ID = 636790;
return o_return;
end jt_test;
declare
l_ids sys_refcursor;
l_id number;
begin
l_ids := jt_test;
loop
fetch l_ids into l_id;
dbms_output.put_line('Rec: ' || l_id);
exit when l_ids%NOTFOUND;
end loop;
close l_ids;
end;

Dynamic cursor Oracle

I want to create a dynamic cursor, but my code does not bring me the correct data. What am I doing wrong?
DECLARE
VAR1 VARCHAR2(500);
CURSOR CUR1 IS
SELECT T.COL1 FROM TABLE1 T WHERE T.COL1 IN (VAR1);
BEGIN
VAR1 := q'['V1','V2']';
FOR REG IN CUR1 LOOP
DBMS_OUTPUT.PUT_LINE(REG.COL1);
END LOOP;
END;
In short, IN clause doesn't support bind variables.. It supports for only value,in the way you used.. You need to specify it like IN (var1, var2);
Without knowing you , you have used bind variables. One workaround is use REFCURSOR By forming a query string dynamically.
DECLARE
VAR1 VARCHAR2(500);
CUR1 SYs_REFCURSOR;
QUERY_STRING VARCHAR2(2000) := 'SELECT T.COL1 FROM TABLE1 T WHERE T.COL1 IN';
MYREC IS RECORD
(
COL1 VARCHAR(1000);
);
myrecord MYREC;
BEGIN
VAR1 := q'['V1','V2']';
QUERY_STRING:= QUERY_STRING||'('||VAR1||')';
OPEN CUR1 FOR QUERy_STRING;
LOOP
FETCH CUR1 INTO myrecord;
DBMS_OUTPUT.PUT_LINE(myrecord.COL1);
EXIT WHEN v_my_ref_cursor%NOTFOUND;
..
-- your processing
END LOOP;
CLOSE CUR1;
END;
One of my other answer also has other way using collections, for bigger IN clause list.
--I USED A TABLE IN OUR DB ORACLE 11G R2
--there are some syntax issues above that I corrected
--I moved the QUERY_STRING after the BEGIN and added output
DECLARE
VAR1 VARCHAR2(500);
CUR1 SYS_REFCURSOR;
QUERY_STRING VARCHAR2(2000);
TYPE MYREC IS RECORD
(
WO_NBR VARCHAR(1000)
);
myrecord MYREC;
BEGIN
VAR1 := q'['45466','45432']';
QUERY_STRING := 'SELECT T.WO_NBR FROM WO_SCHED T WHERE T.WO_NBR IN ('||VAR1||')';
DBMS_OUTPUT.PUT_LINE(VAR1);
DBMS_OUTPUT.PUT_LINE(QUERY_STRING);
OPEN CUR1 FOR QUERY_STRING;
LOOP
FETCH CUR1 INTO myrecord;
DBMS_OUTPUT.PUT_LINE(myrecord.WO_NBR);
EXIT WHEN CUR1%NOTFOUND;
-- your processing
END LOOP;
CLOSE CUR1;
END;

Is there a way to get the parameter values bound to a statement that caused an error from an eventtrigger?

Is there anyway to get the input parameter values, if any, bound to the a statement that caused an error in the database from a trigger? Regardless if the statement in SQL och a procedure/function call.
I'm trying to set up a log of errors on a schema level, which works fine. But I can't find a way to log the parameter values as well, and that would really be helpfull.
So far the trigger I'm using looks like this...
CREATE OR REPLACE TRIGGER t_error
AFTER SERVERERROR
ON SCHEMA
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
l_sql ora_name_list_t;
l_sequence NUMBER;
l_statement CLOB;
l_count NUMBER;
l_ts TIMESTAMP (6);
BEGIN
l_count := ora_sql_txt (l_sql);
IF l_count >= 1 THEN
FOR i IN 1 .. l_count LOOP
l_statement := l_statement || l_sql (i);
END LOOP;
END IF;
INSERT INTO oralog
VALUES (
seq_oralog.NEXTVAL,
dbms_utility.format_call_stack,
dbms_utility.format_error_stack,
dbms_utility.format_error_backtrace,
l_statement,
SYSTIMESTAMP
);
COMMIT;
END t_error;
/
Thanks

Resources