DBMS_SQL.FETCH_ROWS
Hi,
I am seeing a strange thing. See the below code snippet.
v_qry := 'select count(1) from emp where ename = ''SCOT''';
DBMS_OUTPUT.PUT_LINE(V_QRY);
DBMS_SQL.PARSE (cur, v_qry, DBMS_SQL.NATIVE);
int_output := DBMS_SQL.EXECUTE (cur);
int_exec := DBMS_SQL.FETCH_ROWS (cur);
as no row with ename ='SCOT' exists, the int_exec should be 0. It shows 1.
the int_exec should be 0. It shows 1.Check this:
SQL> set serverout on
SQL> DECLARE
cur INT := dbms_sql.open_cursor ();
int_output INT;
int_exec INT;
val_out INT;
v_qry VARCHAR2 (100);
BEGIN
v_qry := 'select count(1) from emp where ename = ''SCOT''';
dbms_output.put_line (v_qry);
dbms_sql.parse (cur, v_qry, dbms_sql.native);
int_output := dbms_sql.execute (cur);
dbms_sql.define_column (cur, 1, val_out);
int_exec := dbms_sql.fetch_rows (cur);
dbms_sql.column_value (cur, 1, val_out);
dbms_output.put_line (int_exec);
dbms_output.put_line (val_out);
dbms_sql.close_cursor (cur);
END;
select count(1) from emp where ename = 'SCOT'
1
0
PL/SQL procedure successfully completed.
Similar Messages
-
ORA-1481 using DBMS_SQL.FETCH_ROWS
Hi all.
I have a package, called from forms 6, that in one of its functions uses DBMS_SQL package.
It has a bunch of DBMS_SQL.DEFINE_COLUMN ....
but fails with ora-1481 in this line (after all the DEFINE_COLUMN)
<quote>
traza := '07 ';
if (DBMS_SQL.FETCH_ROWS(v_cursor)= 0 ) then
traza := '71';
exit;
end if;
traza := '08';
.... more code
<end quote>
The procedure shows, going to the WHEN OTHERS exception, the error 1481 with '07' as traza (trace). Not '71', not '08'.
Someone can help? Thank you all in advance.It could be caused because in the previous DBS_SQL.DEFINE_COLUMN some of the variables used like
- "DBMS_SQL.DEFINE_COLUMN (v_cursor, 71, v_cdclave_sust ,1000 );" -> that (w_cdclave_sust) is a varchar2
- or "DBMS_SQL.DEFINE_COLUMN (v_cursor, 71, v_cdclave_sust_2 );" -> w_cdclave_sust_2 is a number)
are number?
Thanks -
Hello,
I want to write a Procedure that selects the name from the employee which employee-number I give over. But I have one question. How can I give out the ename. Normally if I write a select statement in PL/SQL I must add the INTO-Clause to store the value in a Variable. But how here...
CREATE OR REPLACE PROCEDURE select_table(vempno NUMBER) IS
vename VARCHAR2(30);
rows_processed BINARY_INTEGER;
cur_hdl INTEGER;
stmt_str VARCHAR2(100);
BEGIN
cur_hdl:=DBMS_SQL.OPEN_CURSOR;
stmt_str:='SELECT DISTINCT ename FROM emp WHERE empno'||'=:empno';
DBMS_SQL.PARSE(cur_hdl, stmt_str, dbms_sql.native);
DBMS_SQL.BIND_VARIABLE(cur_hdl,':empno',vempno);
rows_processed:=DBMS_SQL.EXECUTE(cur_hdl);
DBMS_SQL.CLOSE_CURSOR(cur_hdl);
END;Hi,
Try this:
CREATE OR REPLACE PROCEDURE select_table(vempno NUMBER, vempname OUT VARCHAR2) IS
rows_processed BINARY_INTEGER;
cur_hdl INTEGER;
stmt_str VARCHAR2(100);
BEGIN
cur_hdl:=DBMS_SQL.OPEN_CURSOR;
stmt_str:='SELECT DISTINCT ename FROM emp WHERE empno =:empno';
DBMS_SQL.PARSE(cur_hdl, stmt_str, dbms_sql.native);
-- supply binds (bind by name)
DBMS_SQL.BIND_VARIABLE(cur_hdl,'empno',vempno);
-- describe defines
dbms_sql.define_column(cur_hdl, 1, vempname, 200);
-- execute
rows_processed := dbms_sql.execute(cur_hdl);
LOOP
-- fetch a row
IF dbms_sql.fetch_rows(cur_hdl) > 0 then
-- fetch columns from the row
dbms_sql.column_value(cur_hdl, 1, vempname);
ELSE
EXIT;
END IF;
END LOOP;
-- close cursor
dbms_sql.close_cursor(cur_hdl);
END; -
Please help me for peculiar error in Dbms_sql.
Hi im using form 6i
Send query and display output that is my work
sqltext = 'Select a.vendcode,vendname,ap_documentno,ap_documentdate,b.grinno,sum(nvl(landed_cost,0))+sum(nvl(net_rate,0))+sum(nvl(c.cessamount,0))+sum(nvl(vatamount,0)) apGrinval
from billmain a,Billitem b,grinitem c,vendmast d
where a.unitid = :Unit_id
and ap_documentdate between :From_dt and :To_dt
and a.unitid = b.unitid
and a.documentno = b.documentno
and substr(ap_documentno,1,1) <> substr(b.grinno,1,1)
and b.unitid = c.unitid
and b.grinno = c.grinno
and a.vendcode = d.vendcode
and b.partnumber = c.partnumber
group by a.vendcode,ap_documentno,ap_documentdate,b.grinno,vendname';
and i have procedure
PROCEDURE Display_Records(BlockName Varchar2,Sqltext Varchar2) IS
c integer := dbms_sql.open_cursor;
cnt integer;
desctab dbms_sql.desc_tab;
colval varchar(4000);
stat integer;
pitem varchar2(100);
BEGIN
dbms_sql.parse(c,sqltext,1);
Go_Block('Param');
next_item;
Loop
Exit When Upper(Get_item_property(:System.Current_Item,Prompt_Text)) like 'PARAMETER%';
If Substr(:System.Current_value,-5,1) in ('/','.','-') Then
dbms_sql.bind_variable(c,RTrim(Get_item_property(:System.Current_Item,Prompt_Text)),To_date(:System.Current_Value,'dd/mm/yyyy'));
Else
dbms_sql.bind_variable(c,RTrim(Get_item_property(:System.Current_Item,Prompt_Text)),Rtrim(:System.Current_Value));
End If;
next_item;
End Loop;
dbms_sql.describe_columns(c,cnt,desctab);
Go_Block('Data');
First_Record;
For i in 1..cnt Loop
dbms_sql.define_column(c,i,colval,4000);
End Loop;
stat:=dbms_sql.execute(c);
While (dbms_sql.Fetch_rows(c)>0) Loop
pitem := Get_block_property(:System.Current_block,First_item);
For i in 1..cnt Loop
dbms_sql.column_value(c,i,colval);
If colval = '0' Then
colval := null;
End If;
copy(colval,pitem);
Next_Item;
Pitem := :System.Current_Item;
End Loop;
Next_Record;
pause;
End Loop;
Delete_Record;
END;
all th queries r working perfectly but the above query. why i dont know, even the same query without the bold area
its working but result working so I want to include bold creteria.
Nothing error displayed, it is hanging on the area while (dbms_sql.Fetch_rows(c)>0)
but with the same query when i tried in isql*plus it display 70 rows, but here it is hanging.
Please help me what is the error.
kanishWho told dbms_sql only for database. I had send lot of queries using this coding that is working perfectly except which i post.
Yes, I solved it myself just i changed
rtrim(b.partnumber) = rtrim(c.partnumber)
Thank to all
kanish
Edited by: Kanish on May 1, 2009 11:03 PM -
Error when using DBMS_SQL.parse
Has anyone ever ran into this error "ORA-00932: inconsistent datatypes: expected NUMBER got DATE" when using DBMS_SQL.parse? I'm trying to pass in a sql statement that includes date columns but it keeps failing during the parse step. If I put a "to_char" around the dates it works fine.
Any ideas?
declare
l_cursor PLS_INTEGER;
l_rows PLS_INTEGER;
l_col_cnt PLS_INTEGER;
l_desc_tab DBMS_SQL.desc_tab;
l_buffer CLOB;
v_query clob;
l_file UTL_FILE.file_type;
g_sep VARCHAR2(5) := ',';
BEGIN
l_cursor := DBMS_SQL.open_cursor;
v_query := 'SELECT CREATED FROM DBA_USERS';
DBMS_SQL.parse(l_cursor, v_query, DBMS_SQL.native);
DBMS_SQL.describe_columns(l_cursor, l_col_cnt, l_desc_tab);
FOR i IN 1 .. l_col_cnt
LOOP
DBMS_SQL.define_column(l_cursor, i, l_buffer);
END LOOP;
l_rows := DBMS_SQL.execute(l_cursor);
-- Output the column names.
FOR i IN 1 .. l_col_cnt
LOOP
IF i > 1 THEN
UTL_FILE.put(l_file, g_sep);
END IF;
UTL_FILE.put(l_file, l_desc_tab(i).col_name);
END LOOP;
UTL_FILE.new_line(l_file);
-- Output the data.
LOOP
EXIT WHEN DBMS_SQL.fetch_rows(l_cursor) = 0;
FOR i IN 1 .. l_col_cnt
LOOP
IF i > 1 THEN
UTL_FILE.put(l_file, g_sep);
END IF;
DBMS_SQL.COLUMN_VALUE(l_cursor, i, l_buffer);
-- Check for column data type. If "character" data type enclose in quotes
-- 1 = VARCHAR2 and NVARCHAR2, 96 = CHAR and NCHAR, 112 = CLOB
IF l_desc_tab(i).col_type IN (1, 96, 112) THEN
l_buffer := '"' || l_buffer || '"';
END IF;
UTL_FILE.put(l_file, l_buffer);
END LOOP;
UTL_FILE.new_line(l_file);
END LOOP;
UTL_FILE.fclose(l_file);
EXCEPTION
WHEN OTHERS THEN
IF UTL_FILE.is_open(l_file) THEN
UTL_FILE.fclose(l_file);
END IF;
IF DBMS_SQL.is_open(l_cursor) THEN
DBMS_SQL.close_cursor(l_cursor);
END IF;
RAISE;
END;Edited by: jpvybes on Jun 6, 2013 3:47 PM>
Has anyone ever ran into this error "ORA-00932: inconsistent datatypes: expected NUMBER got DATE" when using DBMS_SQL.parse? I'm trying to pass in a sql statement that includes date columns but it keeps failing during the parse step. If I put a "to_char" around the dates it works fine.
>
No - it is NOT failing on the parse step. If you comment out various sections of the code you will find that your loop is causing the problem.
Comment out this loop and there is NO exception.
l_buffer CLOB;
FOR i IN 1 .. l_col_cnt
LOOP
DBMS_SQL.define_column(l_cursor, i, l_buffer);
END LOOP;Do you now see the problem?
You are using 'define_column' and passing 'l_buffer' which is a CLOB. But this is your query
v_query := 'SELECT CREATED FROM DBA_USERS';And that 'CREATED' column in the query is a DATE.
Why are you defining a column as a CLOB when the cursor column is a DATE?
See the example3 in Chapter 100 DBMS_SQL in the Packages and Types doc. It shows an example that includes a DATE column.
http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_sql.htm#i996963 -
Using DBMS_SQL getting ORA-29255
Sort of new to DBMS_SQL - as I've been using NDS for almost everything to this point.
However - I have a situation where I'm selecting data that requires a predicate of multiple values for a single column (i.e. column in (:v1)) where :v1 is a list of values. I was using bind_array to set the values for :v1. However - I get the error above.
The SQL returns mutiple rows so - define array is being used to indicate what column should be returned to what array.
I understand based on the error message that they can't be used together. So- my question is - when using a SQL statement that requires "columnA IN (multiple_values)" how are you supposed to bind those using DBMS_SQL?
Any help is appreciated!!Thanks... Code is below...
You can see where the vspccat is a predicate and - we're trying to get the multiple values 'bound' to the SQL. However - the sql also returns multiple rows (hence the 'define array' to get the return values).
We're using DBMS_SQL over NDS simply because (as isn't illustrated in the example below - which I was using just to get the concept working in an example to use moving forward with the actual code I will need to write) I'm needing to use multiple varrying predicates depending on the situation and - this is a cleaner way to handle that - rather than tracking what's been dynamically written and - handling it with every possible scenario in many different executes (with the different 'using's' attached).
Your help is appreciated!
DECLARE
col1 NUMBER(9,0);
col2 VARCHAR2(15);
col3 VARCHAR2(15);
col4 VARCHAR2(60);
col5 DATE;
col6 DATE;
col7 VARCHAR2(6);
col8 NUMBER(9,0);
col9 VARCHAR2(1);
spc_cat_array DBMS_SQL.VARCHAR2_TABLE;
col1_array DBMS_SQL.NUMBER_TABLE;
col2_array DBMS_SQL.VARCHAR2_TABLE;
col3_array DBMS_SQL.VARCHAR2_TABLE;
col4_array DBMS_SQL.VARCHAR2_TABLE;
col5_array DBMS_SQL.DATE_TABLE;
col6_array DBMS_SQL.DATE_TABLE;
col7_array DBMS_SQL.VARCHAR2_TABLE;
col8_array DBMS_SQL.NUMBER_TABLE;
col9_array DBMS_SQL.VARCHAR2_TABLE;
p_spc_cat VARCHAR2(15) := '03';
p_status VARCHAR2(6) := 'APPROV';
src_cur INTEGER;
dest_cur INTEGER;
ignore INTEGER;
fdbk INTEGER;
l_sql VARCHAR2(1000);
dest_rec VARCHAR2(500);
BEGIN
spc_cat_array(1) := '03';
spc_cat_array(2) := '09';
spc_cat_array(3) := '35';
l_sql := ' SELECT *' ||
' FROM (SELECT maint_id, NULL AS ica_price_rule, spc_category, description,' ||
' effective_date, term_date, status,' ||
' seq_ica_spc_pr_whole AS sequence_id, update_type' ||
' FROM ica_spc_pr_whole_hdr_wrk' ||
' WHERE spc_category IN (:vspccat)' ||
' AND status = :vstatus ' ||
' AND term_date IS NULL' ||
' UNION ALL' ||
' SELECT NULL AS maint_id, NULL AS ica_price_rule, spc_category,' ||
' description, effective_date, term_date, status,' ||
' seq_ica_spc_pr_whole AS sequence_id, NULL AS update_type' ||
' FROM ica_spc_pr_whole_hdr live' ||
' WHERE spc_category IN (:vspccat)' ||
' AND status = :vstatus ' ||
' AND term_date IS NULL' ||
' AND NOT EXISTS (SELECT spc_category' ||
' FROM ica_spc_pr_whole_hdr_wrk wrk' ||
' WHERE wrk.spc_category = live.spc_category)) a';
-- open cursor on source table
src_cur := dbms_sql.open_cursor;
-- parse the SELECT statement
dbms_sql.parse(src_cur, l_sql, dbms_sql.NATIVE);
-- Bind Variables
-- DBMS_SQL.BIND_VARIABLE (src_cur, ':vspccat', p_spc_cat);
DBMS_SQL.BIND_ARRAY (src_cur, ':vspccat', spc_cat_array);
DBMS_SQL.BIND_VARIABLE (src_cur, ':vstatus', p_status);
-- define the column type
dbms_sql.define_array(src_cur, 1, col1_array, 5, 1);
dbms_sql.define_array(src_cur, 2, col2_array, 5, 1);
dbms_sql.define_array(src_cur, 3, col3_array, 5, 1);
dbms_sql.define_array(src_cur, 4, col4_array, 5, 1);
dbms_sql.define_array(src_cur, 5, col5_array, 5, 1);
dbms_sql.define_array(src_cur, 6, col6_array, 5, 1);
dbms_sql.define_array(src_cur, 7, col7_array, 5, 1);
dbms_sql.define_array(src_cur, 8, col8_array, 5, 1);
dbms_sql.define_array(src_cur, 9, col9_array, 5, 1);
ignore := dbms_sql.execute(src_cur);
if dbms_sql.fetch_rows(src_cur) > 0 then
dbms_sql.column_value( src_cur, 1, col1_array);
dbms_sql.column_value( src_cur, 2, col2_array);
dbms_sql.column_value( src_cur, 3, col3_array);
dbms_sql.column_value( src_cur, 4, col4_array);
dbms_sql.column_value( src_cur, 5, col5_array);
dbms_sql.column_value( src_cur, 6, col6_array);
dbms_sql.column_value( src_cur, 7, col7_array);
dbms_sql.column_value( src_cur, 8, col8_array);
dbms_sql.column_value( src_cur, 9, col9_array);
end if;
dbms_output.put_line(col3_array(1));
dbms_sql.close_cursor(src_cur);
end; -
Dbms_sql.parse: varchar2a version does NOT throw ORA-24344
I'm trying to execute some generated code in 10.2. using the varchar2a version of dbms_sql.parse.
This works fine, but when the there is something wrong in the generated code, I do not get an exception (I do get it with the varchar2 version).
So when I run the following example, the first call to dbms_sql.parse creates an invalid package, but only the second call will actually throw an exception:
DECLARE
v_cur INTEGER;
v_sql dbms_sql.varchar2a;
BEGIN
v_cur := DBMS_SQL.open_cursor;
v_sql(1) := 'CREATE OR REPLACE PACKAGE xxx AS a ###; END;'; -- invalid SQL
dbms_sql.parse(v_cur, v_sql, 1, 1, FALSE, dbms_sql.native);
dbms_output.put_line ('ERROR: should have failed');
dbms_sql.parse(v_cur, v_sql(1), dbms_sql.native);
dbms_sql.close_cursor(v_cur);
EXCEPTION
WHEN OTHERS THEN
dbms_sql.close_cursor( v_cur );
dbms_output.put_line ('OK: ' || SQLERRM );
END;
Can anybody reproduce/explain this?
Thanks!Here is the code I am running ...
CREATE OR REPLACE PROCEDURE p_sql_valid_or_not_cnt
(vSQL IN VARCHAR2, vValid OUT NUMBER, vMessage OUT VARCHAR2, vCount OUT NUMBER) IS
-- Purpose: Returns 0 in vvalid if SQL is valid, else returns -1. Returns row count when SQL is valid.
-- Parameters:
-- IN : vSQL
-- OUT : vValid
-- OUT : vMessage
-- OUT : vCount
cur INTEGER := DBMS_SQL.OPEN_CURSOR;
fdbk INTEGER;
BEGIN
DBMS_SQL.PARSE (cur, vSQL, DBMS_SQL.NATIVE);
fdbk := DBMS_SQL.EXECUTE (cur);
vCount := 0;
LOOP /* Fetch next row. Exit when done. */
EXIT WHEN DBMS_SQL.FETCH_ROWS (cur) = 0;
vCount := vCount + 1;
END LOOP;
vValid := 0;
vMessage := 'No errors';
DBMS_SQL.CLOSE_CURSOR (cur);
END p_sql_valid_or_not_cnt;
To run ...
set serveroutput on
declare
i number;
m varchar2(500);
c number;
begin
p_sql_valid_or_not_cnt('SELECT * FROM TAB where 1 <> 1',i,m,c);
DBMS_OUTPUT.PUT_LINE(i);
DBMS_OUTPUT.PUT_LINE(m);
DBMS_OUTPUT.PUT_LINE(c);
end;
This runs fine.
But when I try to access an object from a remote server using DB link. I get the error ...
set serveroutput on
declare
i number;
m varchar2(500);
c number;
begin
p_sql_valid_or_not_cnt('SELECT * FROM REMOTE_SCHEMA.T_REMOTE@REMOTE_SERVER where 1 <> 1',i,m,c);
DBMS_OUTPUT.PUT_LINE(i);
DBMS_OUTPUT.PUT_LINE(m);
DBMS_OUTPUT.PUT_LINE(c);
end;
declare
ERROR at line 1:
ORA-24374: define not done before fetch or execute and fetch
ORA-06512: at "SYS.DBMS_SYS_SQL", line 1125
ORA-06512: at "SYS.DBMS_SQL", line 328
ORA-06512: at "IMEPAS.P_SQL_VALID_OR_NOT_CNT", line 16
ORA-06512: at line 6
I can run the SQL that I am passing as vSQL directly in SQLPLUS ...
This code in itself works.
SELECT * FROM
REMOTE_SCHEMA.T_REMOTE@REMOTE_SERVER
where 1 <> 1;
The user I am using to logon to my server is also there on REMOTE_SERVER.
The DB LINK is created by CONNECTED USER option. The user on REMOTE_SERVER has explict select access on the table (not via role) I am accessing.
Any help would be much appreciated. -
DBMS_SQL.PARSE to access remote objects
I am using following code in a procedure ...
DBMS_SQL.PARSE (cur, vSQL, DBMS_SQL.NATIVE);
where variable vSQL can contain a remote object.
A DB link (with Fixed User option) exists to access that database.
The DB link is working. The remote object is accessible outside this procedure.
When this command is executed, I get ...
ORA-24374: define not done before fetch or execute and fetch
... error.
Can DBMS_SQL handle remote objects?
Oracle version is 9.2.0.5.0
Any help would be greatly appreciated.Here is the code I am running ...
CREATE OR REPLACE PROCEDURE p_sql_valid_or_not_cnt
(vSQL IN VARCHAR2, vValid OUT NUMBER, vMessage OUT VARCHAR2, vCount OUT NUMBER) IS
-- Purpose: Returns 0 in vvalid if SQL is valid, else returns -1. Returns row count when SQL is valid.
-- Parameters:
-- IN : vSQL
-- OUT : vValid
-- OUT : vMessage
-- OUT : vCount
cur INTEGER := DBMS_SQL.OPEN_CURSOR;
fdbk INTEGER;
BEGIN
DBMS_SQL.PARSE (cur, vSQL, DBMS_SQL.NATIVE);
fdbk := DBMS_SQL.EXECUTE (cur);
vCount := 0;
LOOP /* Fetch next row. Exit when done. */
EXIT WHEN DBMS_SQL.FETCH_ROWS (cur) = 0;
vCount := vCount + 1;
END LOOP;
vValid := 0;
vMessage := 'No errors';
DBMS_SQL.CLOSE_CURSOR (cur);
END p_sql_valid_or_not_cnt;
To run ...
set serveroutput on
declare
i number;
m varchar2(500);
c number;
begin
p_sql_valid_or_not_cnt('SELECT * FROM TAB where 1 <> 1',i,m,c);
DBMS_OUTPUT.PUT_LINE(i);
DBMS_OUTPUT.PUT_LINE(m);
DBMS_OUTPUT.PUT_LINE(c);
end;
This runs fine.
But when I try to access an object from a remote server using DB link. I get the error ...
set serveroutput on
declare
i number;
m varchar2(500);
c number;
begin
p_sql_valid_or_not_cnt('SELECT * FROM REMOTE_SCHEMA.T_REMOTE@REMOTE_SERVER where 1 <> 1',i,m,c);
DBMS_OUTPUT.PUT_LINE(i);
DBMS_OUTPUT.PUT_LINE(m);
DBMS_OUTPUT.PUT_LINE(c);
end;
declare
ERROR at line 1:
ORA-24374: define not done before fetch or execute and fetch
ORA-06512: at "SYS.DBMS_SYS_SQL", line 1125
ORA-06512: at "SYS.DBMS_SQL", line 328
ORA-06512: at "IMEPAS.P_SQL_VALID_OR_NOT_CNT", line 16
ORA-06512: at line 6
I can run the SQL that I am passing as vSQL directly in SQLPLUS ...
This code in itself works.
SELECT * FROM
REMOTE_SCHEMA.T_REMOTE@REMOTE_SERVER
where 1 <> 1;
The user I am using to logon to my server is also there on REMOTE_SERVER.
The DB LINK is created by CONNECTED USER option. The user on REMOTE_SERVER has explict select access on the table (not via role) I am accessing.
Any help would be much appreciated. -
Dynamic read from table with DBMS_SQL Block
Hi,
In the orcale8 Application developer's Guide is this example:
-- To select everything from this table and move it into four PL/SQL tables, you could use the following simple program:
c number;
d number;
n_tab dbms_sql.Number_Table;
d_tab1 dbms_sql.Date_Table;
v_tab dbms_sql.Varchar2_Table;
d_tab2 dbms_sql.Date_Table;
indx number := 10;
begin
c := dbms_sql.open_cursor;
dbms_sql.parse(c, 'select * from multi_tab order by 1', EXEC_sql.V7);
dbms_sql.define_array(c, 1, n_tab, 5, indx);
dbms_sql.define_array(c, 2, d_tab1, 5, indx);
dbms_sql.define_array(c, 3, v_tab, 5, indx);
dbms_sql.define_array(c, 4, d_tab2, 5, indx);
d := dbms_sql.execute(c);
loop
d := dbms_sql.fetch_rows(c);
dbms_sql.column_value(c, 1, n_tab);
dbms_sql.column_value(c, 2, d_tab1);
dbms_sql.column_value(c, 3, v_tab);
dbms_sql.column_value(c, 4, d_tab2);
exit when d != 5;
end loop;
dbms_sql.close_cursor(c);
Here the four tables can be used for anything at all. One usage might be to use BIND_ARRAY to move the rows to another table by using a query such as 'INSERT into SOME_T values(:a, :b, :c, :d);
exception when others then
if dbms_sql.is_open(c) then
dbms_sql.close_cursor(c);
end if;
raise;
at the second 'dbms_sql.define_array' comes an ORA-0600 error.
What's wrong ?Some solutions posted here : {message:id=9689485}
-
Reading columns after a "fetch_rows" command
Dear All:
Could somebody help me with this issue?
The following is a sample code to declare, open and fetch a Dynamic Cursor using BIND_ARRAY command ...
declare
c number;
d number;
n_tab dbms_sql.Number_Table;
indx number := -10;
begin
c := dbms_sql.open_cursor;
dbms_sql.parse(c, 'select cod from emp order by 1', dbms_sql);
dbms_sql.define_array(c, 1, n_tab, 10, indx);
d := dbms_sql.execute(c);
loop
d := dbms_sql.fetch_rows(c);
/* The problem is here ...... */
/* After to read the first row, I can not identify a way to read the following row fetched, how can I read the first 10 rows fetched and stored in n_tab variable? */
dbms_sql.column_value(c, 1, n_tab);
exit when d != 10;
end loop;
dbms_sql.close_cursor(c);
exception when others then
if dbms_sql.is_open(c) then
dbms_sql.close_cursor(c);
end if;
raise;
end;
The problem is that I can not identify a way to read each row fetched in n_tab after the fetch_rows command, It's supposed that n_tab store 10 rows each time, but how can I read these 10 rows? Is it enough with the column_value command? after this Do I have to reference each row by n_tab[1], n_tab[2] .... n_tab[10] to read the codes fetched?
Thank you to everybody for your attention
Regards
JosiDear All:
Could somebody help me with this issue?
The following is a sample code to declare, open and fetch a Dynamic Cursor using BIND_ARRAY command ...
declare
c number;
d number;
n_tab dbms_sql.Number_Table;
indx number := -10;
begin
c := dbms_sql.open_cursor;
dbms_sql.parse(c, 'select cod from emp order by 1', dbms_sql);
dbms_sql.define_array(c, 1, n_tab, 10, indx);
d := dbms_sql.execute(c);
loop
d := dbms_sql.fetch_rows(c);
/* The problem is here ...... */
/* After to read the first row, I can not identify a way to read the following row fetched, how can I read the first 10 rows fetched and stored in n_tab variable? */
dbms_sql.column_value(c, 1, n_tab);
exit when d != 10;
end loop;
dbms_sql.close_cursor(c);
exception when others then
if dbms_sql.is_open(c) then
dbms_sql.close_cursor(c);
end if;
raise;
end;
The problem is that I can not identify a way to read each row fetched in n_tab after the fetch_rows command, It's supposed that n_tab store 10 rows each time, but how can I read these 10 rows? Is it enough with the column_value command? after this Do I have to reference each row by n_tab[1], n_tab[2] .... n_tab[10] to read the codes fetched?
Thank you to everybody for your attention
Regards
Josi -
To display values in List Item through Dbms_SQL
Hi,
I want to write the dynamic sql in forms6.0 to built my query at runtime and display the result in List Item. I am getting an error ora-12703. If any person can help me in this problem it will be very happy for me.
declare
l_cursor integer;
l_last_name varchar2(20);
l_createstring1 varchar2(2000) ;
l_first_name varchar2(20) ;
l_id number(9);
result integer;
l_count number(2) := 0;
x varchar2(200);
y number(9);
begin
l_cursor := dbms_sql.open_cursor;
l_createstring1 := 'select id, first_name, last_name from candi_pinfo ' ;
dbms_sql.parse(l_cursor,l_createstring1, 2);
dbms_sql.define_column(l_cursor,1,l_id);
dbms_sql.define_column(l_cursor,2,l_last_name,20);
dbms_sql.define_column(l_cursor,3,l_first_name,20);
result := dbms_sql.execute(l_cursor);
loop
if dbms_sql.fetch_rows(l_cursor) = 0 then
exit;
end if;
dbms_sql.column_value(l_cursor,1,l_id);
dbms_sql.column_value(l_cursor,2,l_last_name);
dbms_sql.column_value(l_cursor,3,l_first_name);
l_count := l_count+1;
add_list_element('candi_id',l_count,l_id| |l_last_name| |l_first_name,l_id);
end loop;
dbms_sql.close_cursor(l_cursor);
:result.candi_id := get_list_element_value('candi_id',1);
exception
when others then
dbms_sql.close_cursor(l_cursor);
raise;
end;
nullHi reddy,
I didn't find any error in your code and I tested in my system by changing query.. and it is working fine.. check declaration of variables datatype ,length etc..
I tested following code ..
declare
l_cursor integer;
l_last_name varchar2(40);
l_createstring1 varchar2(2000) ;
l_first_name varchar2(200) ;
l_id number(9);
result integer;
l_count number(5) := 0;
x varchar2(200);
y number(9);
begin
--clear_list('lb');
l_cursor := dbms_sql.open_cursor;
l_createstring1 := 'select col1,col2 from mytable ' ;
dbms_sql.parse(l_cursor,l_createstring1, 2);
dbms_sql.define_column(l_cursor,1,l_id);
dbms_sql.define_column(l_cursor,2,l_last_name,40);
result := dbms_sql.execute(l_cursor);
loop
if dbms_sql.fetch_rows(l_cursor) > 0 then
dbms_sql.column_value(l_cursor,1,l_id);
dbms_sql.column_value(l_cursor,2,l_last_name);
--dbms_sql.column_value(l_cursor,3,l_first_name);
l_count := l_count+1;
add_list_element('candi_id',l_count,to_char(l_id)| |l_last_name,l_id);
else
exit;
end if;
end loop;
dbms_sql.close_cursor(l_cursor);
:result.candi_id := get_list_element_value('candi_id',1);
exception
when others then
dbms_sql.close_cursor(l_cursor);
end; -
Finding # rows to be returned from dbms_sql
I need to know how many rows are going to be returned from my dbms_sql (without looping). Currently I am going thru a loop until no more rows are left.
LOOP
if dbms_sql.fetch_rows(cursor_handle) > 0 then null;
END LOOP;Unless you have a count(*) in the query, Oracle won't know how many rows are returned by a query until it returns the last row. Why do you need to know the exact count ahead of time?
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC -
Executing a Function in DBMS_SQL
Hi,
Is there any way to dynamically execute a function in DBMS_SQL and get the return value without using SELECT query ? Using the function in SELECT is restrcting DMLs and cause of this below code fails. Please suggest.
+'SELECT function1 v_payload FROM DUAL'+
Can we use something else here ?
CREATE OR REPLACE FUNCTION get_clob ( p_id IN NUMBER)
RETURN CLOB
IS
v_payload CLOB;
v_cursor_id NUMBER;
v_dummy_id NUMBER;
BEGIN
v_cursor_id := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.parse(v_cursor_id, 'SELECT function1 v_payload FROM DUAL', dbms_sql.NATIVE);
DBMS_SQL.define_column(v_cursor_id, 1, v_payload);
DBMS_SQL.bind_variable(v_cursor_id, ':P_ID', p_id, 30);
v_dummy_id := DBMS_SQL.execute(v_cursor_id);
LOOP
-- Fetch a row from the source table
IF DBMS_SQL.fetch_rows(v_cursor_id) > 0 THEN
-- get column values of the row
DBMS_SQL.column_value(v_cursor_id, 1, v_payload);
ELSE
-- No more rows to copy
EXIT;
END IF;
END LOOP;
DBMS_SQL.close_cursor(v_cursor_id);
RETURN v_payload;
EXCEPTION
WHEN OTHERS THEN
IF DBMS_SQL.is_open(v_cursor_id) THEN
DBMS_SQL.close_cursor(v_cursor_id);
END IF;
RAISE;
END;
/Sachin.Singh wrote:
The function throws error,Ooops, I forgot to bind:
SQL> create or replace
2 function function1(
3 p_id number
4 )
5 return clob
6 is
7 v_retval clob;
8 begin
9 select ename
10 into v_retval
11 from emp
12 where empno = p_id;
13 return v_retval;
14 end;
15 /
Function created.
SQL> CREATE OR REPLACE
2 FUNCTION get_clob(
3 p_id IN NUMBER
4 )
5 RETURN CLOB
6 IS
7 v_payload CLOB;
8 v_cursor_id NUMBER;
9 v_dummy_id NUMBER;
10 BEGIN
11 v_cursor_id := DBMS_SQL.OPEN_CURSOR;
12 DBMS_SQL.parse(v_cursor_id,'BEGIN :P_PAYLOAD := function1(:P_ID); END;',dbms_sql.NATIVE);
13 DBMS_SQL.bind_variable(v_cursor_id,':P_ID',p_id);
14 DBMS_SQL.bind_variable(v_cursor_id,':P_PAYLOAD',v_payload);
15 v_dummy_id := DBMS_SQL.execute(v_cursor_id);
16 DBMS_SQL.variable_value(v_cursor_id,'P_PAYLOAD',v_payload);
17 DBMS_SQL.close_cursor(v_cursor_id);
18 RETURN v_payload;
19 EXCEPTION
20 WHEN OTHERS
21 THEN
22 IF DBMS_SQL.is_open(v_cursor_id)
23 THEN
24 DBMS_SQL.close_cursor(v_cursor_id);
25 END IF;
26 RAISE;
27 END;
28 /
Function created.
SQL> SET SERVEROUTPUT ON
SQL> EXEC DBMS_OUTPUT.PUT_LINE(get_clob(7839));
KING
PL/SQL procedure successfully completed.SY. -
Issues with DBMS_SQL.COLUMN_VALUE_LONG
I can't figure out how to exit a loop around COLUMN_VALUE_LONG. I am using this to get the parts of a LONG column, one chunk at a time, and my desire is to exit the loop when all the pieces have been pulled. According to examples that I see online, e.g. AskTom, the last argument "value_length" should return zero once all the parts have been pulled. However; this just isn't working for me.
In the code below, I am incrementing the start postion, i.e. the offset variable within the loop (ln_LongValuePos). I am setting this to itself plus the "value_length" variable (lpi_PieceLength). I am expecting that ln_LongValuePos would get bigger and lpi_PieceLength would return 0 (and thus allow me to exit the loop). Instead, at the top of each loop, ln_LongValuePos is getting reset to 0. It is as if the argument values have been implemented in thw wrong order or something. Can anyone spot what I am doing wrong here?
DECLARE
lclb_ViewSource CLOB;
ls_ViewOwner ALL_VIEWS.OWNER%TYPE;
ls_ViewName ALL_VIEWS.VIEW_NAME%TYPE;
ln_StringFoundPos INTEGER := 0;
ln_LongValuePos INTEGER := 0;
lCsrPtr_ViewSource PLS_INTEGER;
lpi_FetchCnt PLS_INTEGER := 1;
lpi_PieceLength PLS_INTEGER;
ls_ViewSourcePiece VARCHAR2 (32737);
SEARCHS_SOURCE_SQL CONSTANT VARCHAR2 (256)
:= 'SELECT OWNER, VIEW_NAME, TEXT FROM ALL_VIEWS WHERE VIEW_NAME = ' || Chr(39) || 'ECS_CASH' || Chr(39);
SEARCH_STRING CONSTANT VARCHAR2 (32767) := 'CONTRACT_VALUE_FACTOR';
BEGIN
lCsrPtr_ViewSource := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE (lCsrPtr_ViewSource, SEARCHS_SOURCE_SQL, DBMS_SQL.NATIVE);
DBMS_SQL.DEFINE_COLUMN_CHAR (lCsrPtr_ViewSource,
1,
ls_ViewOwner,
30);
DBMS_SQL.DEFINE_COLUMN_CHAR (lCsrPtr_ViewSource,
2,
ls_ViewName,
30);
DBMS_SQL.DEFINE_COLUMN_LONG (lCsrPtr_ViewSource, 3);
lpi_FetchCnt := DBMS_SQL.EXECUTE_AND_FETCH (lCsrPtr_ViewSource);
WHILE (lpi_FetchCnt > 0)
LOOP
DBMS_SQL.COLUMN_VALUE_CHAR( lCsrPtr_ViewSource, 1, ls_ViewOwner );
DBMS_SQL.COLUMN_VALUE_CHAR( lCsrPtr_ViewSource, 2, ls_ViewName );
ls_ViewOwner := Trim( ls_ViewOwner );
ls_ViewName := Trim( ls_ViewName );
DBMS_LOB.CREATETEMPORARY (lclb_ViewSource, FALSE, 2);
DBMS_LOB.OPEN (lclb_ViewSource, DBMS_LOB.LOB_READWRITE);
LOOP
DBMS_SQL.COLUMN_VALUE_LONG (lCsrPtr_ViewSource,
3,
32767,
ln_LongValuePos,
ls_ViewSourcePiece,
lpi_PieceLength);
EXIT WHEN lpi_PieceLength = 0;
ln_LongValuePos := ln_LongValuePos + lpi_PieceLength;
DBMS_LOB.WRITEAPPEND (lclb_ViewSource,
lpi_PieceLength,
UPPER (ls_ViewSourcePiece));
END LOOP;
ln_LongValuePos := 0;
ln_StringFoundPos := DBMS_LOB.INSTR (lclb_ViewSource, SEARCH_STRING);
IF (ln_StringFoundPos > 0)
THEN
DBMS_OUTPUT.PUT_LINE (
'OWNER: ' || ls_ViewOwner || ' VIEW: ' || ls_ViewName
END IF;
DBMS_LOB.CLOSE (lclb_ViewSource);
DBMS_LOB.FREETEMPORARY (lclb_ViewSource);
lpi_FetchCnt := DBMS_SQL.EXECUTE_AND_FETCH (lCsrPtr_ViewSource);
END LOOP;
DBMS_SQL.CLOSE_CURSOR (lCsrPtr_ViewSource);
END;-Joe
Edited by: Joe Upshaw on Sep 10, 2012 9:44 AMWhy are you issuing DBMS_SQL.EXECUTE_AND_FETCH second time in a loop? This would create an infinite loop. You need to execute DBMS_SQL.FETCH_ROWS within a loop. Also, there is no need to use DBMS_LOB. Just use CLOB directly. For example:
DECLARE
lclb_ViewSource CLOB;
ls_ViewOwner ALL_VIEWS.OWNER%TYPE;
ls_ViewName ALL_VIEWS.VIEW_NAME%TYPE;
ln_StringFoundPos INTEGER := 0;
ln_LongValuePos INTEGER := 0;
lCsrPtr_ViewSource PLS_INTEGER;
lpi_FetchCnt PLS_INTEGER := 1;
lpi_PieceLength PLS_INTEGER;
ls_ViewSourcePiece VARCHAR2 (32737);
SEARCHS_SOURCE_SQL CONSTANT VARCHAR2 (256) := 'SELECT OWNER, VIEW_NAME, TEXT FROM DBA_VIEWS WHERE VIEW_NAME LIKE ''%TAB_COLS''';
SEARCH_STRING CONSTANT VARCHAR2 (32767) := '_CURRENT_EDITION_OBJ';
BEGIN
lCsrPtr_ViewSource := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE (lCsrPtr_ViewSource, SEARCHS_SOURCE_SQL, DBMS_SQL.NATIVE);
DBMS_SQL.DEFINE_COLUMN_CHAR(
lCsrPtr_ViewSource,
1,
ls_ViewOwner,
30
DBMS_SQL.DEFINE_COLUMN_CHAR(
lCsrPtr_ViewSource,
2,
ls_ViewName,
30
DBMS_SQL.DEFINE_COLUMN_LONG(
lCsrPtr_ViewSource,
3
lpi_FetchCnt := DBMS_SQL.EXECUTE_AND_FETCH(lCsrPtr_ViewSource);
WHILE lpi_FetchCnt > 0 LOOP
DBMS_SQL.COLUMN_VALUE_CHAR( lCsrPtr_ViewSource, 1, ls_ViewOwner );
DBMS_SQL.COLUMN_VALUE_CHAR( lCsrPtr_ViewSource, 2, ls_ViewName );
ls_ViewOwner := Trim( ls_ViewOwner );
ls_ViewName := Trim( ls_ViewName );
LOOP
DBMS_SQL.COLUMN_VALUE_LONG (lCsrPtr_ViewSource,
3,
32767,
ln_LongValuePos,
ls_ViewSourcePiece,
lpi_PieceLength);
EXIT WHEN lpi_PieceLength = 0;
ln_LongValuePos := ln_LongValuePos + lpi_PieceLength;
lclb_ViewSource := lclb_ViewSource || UPPER(ls_ViewSourcePiece);
END LOOP;
ln_LongValuePos := 0;
ln_StringFoundPos := INSTR(lclb_ViewSource,SEARCH_STRING);
IF (ln_StringFoundPos > 0)
THEN
DBMS_OUTPUT.PUT_LINE (
'OWNER: ' || ls_ViewOwner || ' VIEW: ' || ls_ViewName
END IF;
lpi_FetchCnt := DBMS_SQL.FETCH_ROWS(lCsrPtr_ViewSource);
END LOOP;
END;
OWNER: SYS VIEW: USER_TAB_COLS
OWNER: SYS VIEW: USER_TSTZ_TAB_COLS
OWNER: SYS VIEW: USER_XML_TAB_COLS
OWNER: SYS VIEW: ALL_TAB_COLS
OWNER: SYS VIEW: ALL_TSTZ_TAB_COLS
OWNER: SYS VIEW: ALL_XML_TAB_COLS
OWNER: SYS VIEW: DBA_TAB_COLS
OWNER: SYS VIEW: DBA_TSTZ_TAB_COLS
PL/SQL procedure successfully completed.
SQL> SY. -
Command not found error while executing a shell script
Hello,
I am a newbie to linux.I am attaching the code which gives me following errors..
error list:
1. no such file or directory enviornemnt
2. command not found
3. ambiguous redirectline
Script
cd $HOME/wkdir
rm /tmp/*.log
# source environment
. ./env
# Run the install script to setup the database
# Configure SH account
sqlplus "/ as sysdba" <<! > /tmp/perflab_install.log 2>&1
grant connect, resource, dba to SH;
alter user sh account unlock;
# create the fetch_n_rows procedure
sqlplus "$PERFLAB_USER" <<! >> /tmp/perflab_install.log 2>&1
drop index sales_time_bix;
drop index sales_time_idx;
create index sales_time_idx on sales(time_id) compute statistics;
-- fetch_n_rows: fetches 'n' rows from the specified statement --
CREATE OR REPLACE PROCEDURE fetch_n_rows(
stmt VARCHAR,
name VARCHAR,
nexec NUMBER := 1,
nrows NUMBER := 0,
debug BOOLEAN := FALSE)
IS
-- Local variables
curs INTEGER := null;
rc INTEGER;
nexec_it INTEGER := 0;
nrows_it INTEGER;
BEGIN
dbms_application_info.set_module('DEMO', name);
WHILE (nexec_it < nexec)
LOOP
curs := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(curs, stmt, DBMS_SQL.NATIVE);
rc := DBMS_SQL.EXECUTE(curs);
nrows_it := 0;
LOOP
IF (dbms_sql.fetch_rows(curs) <= 0 OR (nrows <> 0 AND nrows_it = nrows
THEN
EXIT;
ELSE IF (debug = TRUE)
THEN
DBMS_OUTPUT.PUT_LINE(nrows_it);
END IF;
END IF;
nrows_it := nrows_it + 1;
END LOOP;
DBMS_SQL.CLOSE_CURSOR(curs);
nexec_it := nexec_it + 1;
END LOOP;
dbms_application_info.set_module(null, null);
END fetch_n_rows;
show errors
# Start the workload
. ./start_workload.sh > /tmp/setup_perflab.log 2>&1
# Wait two minutes for workload to get going
sleep 120
# Modify snapshot interval
sqlplus -s /NOLOG <<EOF >> /tmp/setup_perflab.log 2>&1
connect / as sysdba
set head on
set feedback on;
set pagesize 40
rem -- event to allow setting very short Flushing interval
alter session set events '13508 trace name context forever, level 1';
rem -- change INTERVAL setting to 2 minutes
rem -- change RETENTION setting to 6 hours (total of 180 snapshots)
execute dbms_workload_repository.modify_snapshot_settings(interval => 2,-
retention => 360);
EOF
Note : start_workload.sh is also in the same directory..
Any help would be greatly appreciated.Please put your script between "code" format delimiters.
http://wiki.oracle.com/page/Oracle+Discussion+Forums+FAQ
You can add the line "set -x" at the beginning of your script to see at which line it fails? I suspect the . ./env line.
Maybe you are looking for
-
How to use one Datasource name with different pools
Hi, I am using WLS 814 with 4 managed servers. Our application uses (Entity) EJBs with a defined datasource name. Each managed server should have its own connection pool pointing to a separate database. Now I want to deploy the same EAR on all server
-
Run Python Script in Automator
I have a python script (which was written for me), and I would like to make it so that the script executes every x minutes. I know this should be simple to do, but I can't figure it out. Thus far, I have created a workflow in automator, used the "Run
-
Sending a smartform output as an attachment in RTF format via E-mail
Hi all, I have a scenario in which the output displayed using smartforms is to be sent as a e-mail attachment. Also, the attachment should be a word doc(RTF format). Can you please me with this. Warm Regards, Abhijeet
-
Hello, I am trying to find a code to find all the references of the property defined in a class which is very similar to 'Find All References' function available in Visual Studio environment. But I want to have it in code during run time. How to do
-
WPC: Access Java beans from XSL
Hi, How can we access custom Java beans from within the XSLs used to render WPC webforms? Do we have to implement a custom XSLT Helper? I am able to access standard Java classes using the <xmlns> tag but when I try to reference our custom classes the