DBMS_SQL
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;
Similar Messages
-
Sql is too long for DBMS_SQL.EXECUTE()
The code as the following works. However if I run it with my original SQL statement(in quotes), this procedure does not have results shown. How to solve the problem?
create or replace procedure dynamic_sql(p_fail IN tab.DisFai1_1%type) IS
v_counter NUMBER;
v_sqlClause varchar2(1000);
v_cursorID INTEGER;
v_return_rows INTEGER;
v_c number;
BEGIN
v_cursorID := DBMS_SQL.OPEN_CURSOR;
FOR i IN 1..17 LOOP
/* v_sqlClause := 'BEGIN select count(*) from tab where DISFAI'||i||'_1 LIKE :f OR DISFAI'||i||'_2 LIKE :f OR DISFAI'||i||'_3 LIKE :f OR DISFAI'||i||'_4 LIKE :f OR DISFAI'||i||'_5 LIKE :f OR DISFAI'||i||'_6 LIKE :f OR DISFAI'||i||'_7 LIKE :f OR DISFAI'||i||'_8 LIKE :f OR DISFAI'||i||'_9 LIKE :f;END;';*/
v_sqlClause := 'BEGIN select count(*) INTO :counter from bhu where DISFAI'||i||'_1 LIKE :f;END;';
DBMS_OUTPUT.PUT_LINE('SQL= '||v_sqlClause);
DBMS_SQL.PARSE(v_cursorID, v_sqlClause, DBMS_SQL.V7);
dbms_sql.bind_variable(v_cursorID, ':f', p_fail );
dbms_sql.bind_variable(v_cursorID, ':counter', v_counter );
v_return_rows := DBMS_SQL.EXECUTE(v_cursorID);
dbms_sql.variable_value(v_cursorID, ':counter', v_counter );
DBMS_OUTPUT.PUT_LINE('there are '||v_counter||' rows.');
END LOOP;
dbms_sql.close_cursor(v_cursorID);
EXCEPTION
WHEN OTHERS THEN
DBMS_SQL.CLOSE_CURSOR(v_cursorID);
END;Christian was referring to EXECUTE IMMEDIATE
declare
sql_stmt VARCHAR2(200);
v_count PLS_INTEGER;
v_deptno PLS_INTEGER := 20;
begin
sql_stmt := 'SELECT count(*) FROM emp WHERE deptno = :id';
EXECUTE IMMEDIATE sql_stmt INTO v_count USING v_deptno;
dbms_output.put_line(v_count);
end;
/For more information see PL/SQL User's Guide and Reference Chapter 10
http://download-west.oracle.com/docs/cd/A87860_01/doc/appdev.817/a77069/10_dynam.htm#4376 -
DBMS_SQL.TO_REFCURSOR and dynamic sql
Hello,
We've stored procedures that execute dynamic sql statements, an example template given below:
create procedure sp_procedure1 (
P_pselect_txt IN NVARCHAR2 DEFAULT NULL
P_CV1 IN OUT PK_COM_DEFS.CV_TYP ) -- cv_typ is a loosely typed ref cursor.
As
begin
open p_cv1 for p_select_txt;
end;
Now the issue is, open cursor do not support NVARCHAR2 data type. So, trying to use a new feature in 11g, DBMS_SQL.TO_REFCURSOR.
Changed the above procedure as below:
create procedure sp_procedure1 (
P_pselect_txt IN NVARCHAR2 DEFAULT NULL
P_CV1 IN OUT PK_COM_DEFS.CV_TYP ) -- cv_typ is a loosely typed ref cursor.
As
begin
c number;
r number;
c:= dbms_sql.open_cursor;
dbms_sql.parse (c, P_pselect_txt, dbms_sql.native);
r := dbms_sql.execute(c);
p_cv1 := dbms_sql.to_refcursor(c);
dbms_sql.close_cursor (c);
end;
but getting "statement handle not executed" & access denied on sql.dbms_sql error messages.
Could someone suggest how to change the above procedure to use dbms_sql.to_refcursor?
thanks
K.SQL> CREATE TABLE ntest (
2 mycol NVARCHAR2(30));
Table created.
SQL> DECLARE
2 rec_array SYS_REFCURSOR;
3 BEGIN
4 OPEN rec_array FOR
5 'SELECT mycol FROM ntest';
6 END;
7 /
PL/SQL procedure successfully completed.If you can not do this in your version then please post the results of this query:
SELECT * FROM v$version; -
Getting ORA-01001: invalid cursor when I try to parse my SQL statement with DBMS_SQL
-- To modify this template, edit file PROC.TXT in TEMPLATE
-- directory of SQL Navigator
-- Purpose: Briefly explain the functionality of the procedure
-- MODIFICATION HISTORY
-- Person Date Comments
TYPE column_find IS VARRAY(999) of VARCHAR2(4);
v_client_column varchar2(4);
v_errorcode NUMBER;
v_errortext VARCHAR2(200);
i number :=0;
V_COLUMN_NAME varchar2(20);
V_COLUMN_NAME2 varchar2(20);
v_SQL_STMT varchar2(2000);
v_c_hndl integer;
v_Client_ID varchar2(200);
column_name varchar2(20);
execute_feedback integer;
mal material_attribute_pre_load%
ROWTYPE;
local_var varchar2(200);
clienttab DBMS_SQL.DATE_TABLE;
client_column column_find := column_find();
-- Declare program variables as shown above
BEGIN
--client_column := column_find();
v_c_hndl := DBMS_SQL.OPEN_CURSOR;
while i < 999
LOOP
i := i + 1;
client_column.EXTEND;
client_column(i) := CONCAT('c',i);
dbms_output.put_line(client_column(i));
BEGIN
dbms_output.put_line('Starting SQL_STMT.');
v_SQL_STMT := 'SELECT :column_name FROM
MATERIAL_ATTRIBUTE_PRE_LOAD WHERE :column_name2 = :v_Client_ID';
dbms_output.put_line('Starting DBMS_SQL.PARSE.');
DBMS_SQL.PARSE(v_c_hndl,v_SQL_STMT,DBMS_SQL.NATIVE);
dbms_output.put_line('Starting DBMS_SQL.BIND_VARIABLE.');
DBMS_SQL.BIND_VARIABLE
(v_c_hndl,'v_Client_ID','Client ID');
DBMS_SQL.BIND_VARIABLE
(v_c_hndl,'column_name',client_column(i));
DBMS_SQL.BIND_VARIABLE
(v_c_hndl,'column_name2',client_column(i));
dbms_output.put_line('Starting DBMS_SQL.DEFINE_ARRAY.');
DBMS_SQL.DEFINE_ARRAY(v_c_hndl,1,clienttab,999,1);
execute_feedback := DBMS_SQL.EXECUTE_AND_FETCH
(v_c_hndl);
DBMS_SQL.CLOSE_CURSOR(v_c_hndl);
dbms_output.put_line
EXCEPTION
WHEN OTHERS THEN
v_errorcode := SQLCODE;
v_errortext := SUBSTR(SQLERRM,1,200);
dbms_output.put_line('DISPLAY_ERROR'||SUBSTR(TO_CHAR
(v_errorcode)||v_errortext,1,30));
END;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
v_errorcode := SQLCODE;
v_errortext := SUBSTR(SQLERRM,1,200);
dbms_output.put_line('DISPLAY_ERROR'||SUBSTR(TO_CHAR
(v_errorcode)||v_errortext,1,30));
END; -- Procedure COLUMN_FINDI'm using Oracle Database Express edition (latest version from the website afaik), logging into the web interface (using unique account I created after setup[not System], gave it All rights), clicking on SQL > SQL Commands > Enter commands.
I can't run each insert individually, that is only 1 table out of 8 tables, and I probably need to be able to run the entire script (which is actually headed by a sequence of Drop Table commands) sometimes, to refresh the data back to original values in case I stuff things up. The code was originally provided in a text file called bookscript.sql, but I haven't had any success importing or uploading that file into Oracle XE, if I use SQL > SQL Scripts > Upload, the file uploads, I then click on it and click on Run, but nothing seems to happen. (File is shared [read-only] here: https://docs.google.com/leaf?id=0B7IgBXIBNxw3OTY1ZTdmNGQtMjEwYi00YmRmLTkwZjItODZkYmYxODNkMTk4&hl=en_US).
Would love any guidance as to how to modify my environment or how to input the code differently to get it running, it works fine in the hosted environment at Uni, it's an Oracle class (textbook is Guide to Oracle 10g). Lecturer doesn't seem to know much about setting up Oracle, only working in a functioning environment. -
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 -
Dbms_sql in a different schema from query table-error ** ORA-00942
Oracle Experts,
I think I am having problems with using DBMS_SQL in which the function was created in one schema and the query table was created in a different schema.
We have 2 schemas: S1, S2
We have 2 tables:
T1 in Schema S1
T2 in Schema S2
We have a function F1 created by DBA in schema S1 that uses the dbms_sql as:
CREATE OR REPLACE FUNCTION S1.F1(v1 in VARCHAR2) return NUMBER IS
cursor1 INTEGER;
BEGIN
cursor1 := dbms_sql.open_cursor;
dbms_sql.parse(cursor1, v1, dbms_sql.NATIVE);
dbms_sql.close_cursor(cursor1);
return (0);
EXCEPTION
when others then
dbms_sql.close_cursor(cursor1);
return (1) ;
END;
I am using jdeveloper 11G. We have an Oracle DB 11g.
We have a java program which uses jdbc to talk to our Oracle DB.
Basically, in my java program, I call function F1 to check if the query is valid.
If it is, it returns 0. Otherwise, returns 1:
oracle.jdbc.OracleCallableStatement cstmt = (oracle.jdbc.OracleCallableStatement) connection.prepareCall ("begin ? := S1.F1 (?); end;");
cstmt.registerOutParameter (1, java.sql.Types.INTEGER);
cstmt.setString(2, "Select * from S2.T2");
cstmt.execute ();
Since the table that I run the query is T2, created in different schema than F1 was created in, I have the error:
** ORA-00942: table or view does not exist
So my questions are these:
- I am using Oracle DB 11g, if I run the query on a table that created in a different schema from the one that the function (which uses dbms_sql) was created in, I would get the error ORA-00942?
- If I runs the query on table T1 in the same schema as the function F1, would I have the same problem(The reason I ask is I cannot create any table in schema S1 because the DBA has to do it; I am not a DBA)
- This is not a problem, but a security feature because of SQL injection?
- How to resolve this issue other than creating the table in the same schema as the function that utilizes DBMS_SQL?
Regards,
BinhDefiner rights (default) stored objects run under owner's security domain and ignore role based privileges. So regardless what user you are logged in as, function S1.F1 always executes as user S1 and ignores user S1 roles. Therefore exeuting statement within S1.F1:
Select * from S2.T2requires user S1 to have SELECT privilege on S2.T2 granted to S1 directly, not via role.
SY. -
ORA-01704 error while running dbms_sql for a clob
Hi,
I'm running following code and getiing error ORA-01704: string literal too long
declare
cursor_name INTEGER;
t1 clob;
t2 clob;
r number;
begin
select a1 into t1 from temp
where n1 = 'test';
t2 := 'insert into temp (a1,n1) values (''' || t1 || ''',''test12'')' ;
cursor_name := dbms_sql.open_cursor;
DBMS_SQL.PARSE(cursor_name, t2, DBMS_SQL.NATIVE);
r := DBMS_SQL.EXECUTE(cursor_name);
DBMS_SQL.CLOSE_CURSOR(cursor_name);
dbms_output.put_line(r);
end;
clob t1 is 32598 butes long.
in temp table a1 is a clob.
Could anyone please tell me why the error and how to solve the problem.
Thanks
TarunThis is one of the many posts over here where the OP can not be bothered to include the four digit version number.
I can never understand why the version number is left out other than out of rudeness, arrogance and utter laziness.
Apparently the small lot here responding should be clairvoyant and/or remember all version numbers.
I'm going to call for a boycot of people posting without version number!!!!
As to the code:
There is no reason why you are using dbms_sql here, you are misusing Oracle, and it should have been static sql.
At least t2 should have been a bind variable, dbms_sql does support bind variables!
As far as I know the command string should be a varchar2, not a clob, and if t2 is a bind variable there is no need for t1 to be a clob.
Sybrand Bakker
Senior Oracle DBA -
Error while executing query longer thank 32K using dbms_sql
Hi,
I'm using oracle database version is 10.2.0.4.0
I've a query of size 41K.
I can't run it with execute immediate, so when I'm running it with dbms_sql I'm getting error -
ORA-06502: PL/SQL: numeric or value error.
But when I'm running it in my pl/sql developer it is running fine.
Code snippet is below
-- Created on 24/05/2010 by TSHARMA
declare
-- Local variables here
cursor_name INTEGER;
i integer;
tc clob;
begin
-- Test statements here
select col1 into tc from temp where a1 = 't1'; --getting the long query from a temp table
tc := 'create table temp1 as ' || tc;
cursor_name := dbms_sql.open_cursor;
DBMS_SQL.PARSE(cursor_name, tc, DBMS_SQL.NATIVE);
i := DBMS_SQL.EXECUTE(cursor_name);
DBMS_SQL.CLOSE_CURSOR(cursor_name);
end;
-------------------------------Hi,
DBMS_SQL.PARSE is overloaded to accept a VARCHAR2A as the statement parameter. This is a table of VARCHAR2(32767) which can be built up using your long query. I've written the following to handle long CLOB's. Just call the execute_sql procedure passing in the long sql:
PROCEDURE execute_sql (p_sql IN CLOB) IS
l_sql_table DBMS_SQL.VARCHAR2a;
l_ds_cursor PLS_INTEGER;
l_ret_val PLS_INTEGER;
BEGIN
l_sql_table := populate_sql_table(p_sql => p_sql);
l_ds_cursor := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(l_ds_cursor, l_sql_table, l_sql_table.FIRST, l_sql_table.LAST, FALSE, DBMS_SQL.NATIVE);
l_ret_val := DBMS_SQL.EXECUTE(l_ds_cursor);
DBMS_SQL.CLOSE_CURSOR(l_ds_cursor);
END execute_sql;
FUNCTION populate_sql_table (p_sql IN CLOB) RETURN DBMS_SQL.VARCHAR2a IS
l_buffer_length CONSTANT PLS_INTEGER := 32767;
l_lob_length PLS_INTEGER;
l_offset PLS_INTEGER := 1;
l_amount PLS_INTEGER := l_buffer_length;
l_sql_table DBMS_SQL.VARCHAR2a;
BEGIN
l_sql_table.DELETE;
l_lob_length := DBMS_LOB.GETLENGTH(p_sql);
LOOP
IF l_buffer_length > l_lob_length THEN
l_amount := l_lob_length;
END IF;
l_sql_table(NVL(l_sql_table.LAST, 0) + 1) := DBMS_LOB.SUBSTR(p_sql, l_amount, l_offset);
l_offset := l_offset + l_buffer_length;
l_lob_length := l_lob_length - l_amount;
IF l_lob_length <= 0 THEN
EXIT;
END IF;
END LOOP;
RETURN(l_sql_table);
END populate_sql_table;Hope that helps -
We are migrating forms from forms6i to 9i
In forms 6i they are using the below
code.
l_cursor := dbms_sql.open_cursor;
dbms_sql.parse(l_cursor, l_stmt,2);
l_result := dbms_sql.execute(l_cursor);
dbms_sql.close_cursor(l_cursor);
In forms9i this is giving a compilation error.
I created a procedure in the database with this code
and calling the procedure in my forms9i, it is not giving compilation error, but at runtime it is giving this error
Error ORA-00942: table or view does not exist
ORA-06512: at "SYS.DBMS_SYS_SQL line 826
ORACE-06512: at "SYS.DBMS_SQL", line 39
ORA-06512: at "scott.sample" line 6
please suggest what to use instead of dbms_sql to solve this problemDBMS_SQL is a server-side package, and DBMS_SQL.Native is a package variable. Server-side package variables cannot be accessed from forms. You can substitute the number 1 for the variable, though.
However, Oracle does not support using dbms_sql from Forms, anyway. You can use Exec_SQL, which is a Forms-side package, and it is almost the same as DBMS_SQL.
However, if you are just issuing some DDL, Forms_DDL will work fine. It is only when you want to retrieve data from the server that you need to use Exec_SQL. -
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 -
Implementing snapshot replication using pl/sql and dbms_sql
Hi All,
I'm trying to implement snapshot refresh programmatically using a control table to trigger the event and a separate pl/sql stored procedure for each table refresh. I can drop and re-create the snapshots outside of pl/sql with no problem. Once in pl/sql, I'm using the dbms_sql package to wrap the snapshot drop and create statements. The drop statement works fine but I'm getting the following error on the create statement:
BEGIN pk_manage_data_replication.sp_refresh_lcanswertype; END;
ERROR at line 1:
ORA-01031: insufficient privileges
ORA-06512: at "SYS.DBMS_SYS_SQL", line 824
ORA-06512: at "SYS.DBMS_SQL", line 32
ORA-06512: at "AIRES_REFP.PK_MANAGE_DATA_REPLICATION", line 75
ORA-06512: at line 1
The stored procedure code is as follows:
procedure sp_refresh_lcanswertype is
cid integer;
begin
-- drop the snapshot
cid := dbms_sql.open_cursor;
dbms_sql.parse (cid, 'drop snapshot lcanswertype', dbms_sql.v7);
dbms_sql.close_cursor(cid);
-- recreate the snapshot
cid := dbms_sql.open_cursor;
dbms_sql.parse (cid, 'create snapshot lcanswertype
pctfree 5
tablespace lcdata as
select id
,possibleanswers
,creatorptr
,datecreated
,pk_manage_data_replication.sf_get_moddate(''LCANSWERTYPE'',moddate) moddate
from lcanswertype@airess1', dbms_sql.v7);
dbms_sql.close_cursor(cid);
end sp_refresh_lcanswertype;
Any ideas???
Thanks,
JimAn old chestnut. You have been granted the privilege CREATE SNAPSHOT through a role (probably CONNECT). We can only build procedures (and views) on privileges that have been granted to the uuser directly.
Cheers, APC -
How to get SQL text from dbms_sql cursor ID
Hello,
I have a progam using dbms_sql. A cursor is built by dbms_sql.parse (cur_id, v_query, dbms_sql.native);From now on I have only the internal ID cur_id of the cursor. Is there a way to get back the original query the cursor is built from when the cursor is passed to another package?
Regards
MarcusOracle provides several views that show part or all of the SQL statement. See your Oracle version# Reference manual for v$sql...
v$sqltext is one view that displays the full text but the text is contained in multiple rows.
HTH -- Mark D Powell -- -
How to make dynamic query using DBMS_SQL variable column names
First of all i will show a working example of what i intend to do with "EXECUTE IMMEDIATE":
(EXECUTE IMMEDIATE has 32654 Bytes limit, which isn't enough for me so i'm exploring other methods such as DBMS_SQL)
-------------------------------------------------CODE-----------------------------------
create or replace PROCEDURE get_dinamic_query_content
(query_sql IN VARCHAR2, --any valid sql query ('SELECT name, age FROM table')
list_fields IN VARCHAR2) --list of the columns name belonging to the query ( arr_list(1):='name'; arr_list(2):='age';
-- FOR k IN 1..arr_list.count LOOP
-- list_fields := list_fields || '||content.'||arr_list(k)||'||'||'''~cs~'''; )
AS
sql_stmt varchar (30000);
BEGIN
sql_stmt :=
'DECLARE
counter NUMBER:=0;
auxcontent VARCHAR2(30000);
CURSOR content_cursor IS '|| query_sql ||';
content content_cursor%rowtype;
Begin
open content_cursor;
loop
fetch content_cursor into content;
exit when content_cursor%notfound;
begin
auxcontent := auxcontent || '||list_fields||';
end;
counter:=counter+1;
end loop;
close content_cursor;
htp.prn(auxcontent);
END;';
EXECUTE IMMEDIATE sql_stmt;
END;
-------------------------------------------------CODE-----------------------------------
I'm attepting to use DBMS_SQL to perform similar instructions.
Is it possible?Hi Pedro
You need to use DBMS_SQL here because you don't know how many columns your query is going to have before runtime. There are functions in DBMS_SQL to get information about the columns in your query - all this does is get the name.
SQL> CREATE OR REPLACE PROCEDURE get_query_cols(query_in IN VARCHAR2) AS
2 cur PLS_INTEGER;
3 numcols NUMBER;
4 col_desc_table dbms_sql.desc_tab;
5 BEGIN
6 cur := dbms_sql.open_cursor;
7 dbms_sql.parse(cur
8 ,query_in
9 ,dbms_sql.native);
10 dbms_sql.describe_columns(cur
11 ,numcols
12 ,col_desc_table);
13 FOR ix IN col_desc_table.FIRST .. col_desc_table.LAST LOOP
14 dbms_output.put_line('Column ' || ix || ' is ' ||
15 col_desc_table(ix).col_name);
16 END LOOP;
17 dbms_sql.close_cursor(cur);
18 END;
19 /
Procedure created.
SQL> exec get_query_cols('SELECT * FROM DUAL');
Column 1 is DUMMY
PL/SQL procedure successfully completed.
SQL> exec get_query_cols('SELECT table_name, num_rows FROM user_tables');
Column 1 is TABLE_NAME
Column 2 is NUM_ROWS
PL/SQL procedure successfully completed.
SQL> exec get_query_cols('SELECT column_name, data_type, low_value, high_value FROM user_tab_cols');
Column 1 is COLUMN_NAME
Column 2 is DATA_TYPE
Column 3 is LOW_VALUE
Column 4 is HIGH_VALUE
PL/SQL procedure successfully completed.I've just written this as a procedure that prints out the column names using dbms_output - I guess you're going to do something different with the result - maybe returning a collection, which you'll then parse through in Apex and print the output on the screen - this is just to illustrate the use of dbms_sql.
best regards
Andrew
UK -
How to handle error while using dbms_sql.execute
Hi,
I am inserting some records by using the following piece of code.
stmt := 'insert into SSI_KPI_GOAL_VALUE_H (KPI_VAL_KPI_ID, KPI_VAL_RM_CDE,'|| v_day_value ||',KPI_VAL_ACT_DLY,'||v_month_val||',KPI_VAL_BIZ_UNIT_CDE) values (:kpi_array,:rm_array,:day1_array,:day1_array,:day1_array,:busnunit_array)';
l := dbms_sql.open_cursor;
dbms_sql.parse(l, stmt, dbms_sql.native);
dbms_sql.bind_array(l, ':kpi_array', col1_ins,1,ins_cnt-1);
dbms_sql.bind_array(l, ':rm_array', col2_ins,1,ins_cnt-1);
dbms_sql.bind_array(l, ':day1_array', col3_ins,1,ins_cnt-1);
dbms_sql.bind_array(l, ':busnunit_array', col4_ins,1,ins_cnt-1);
dummy := dbms_sql.execute(l);
dbms_sql.close_cursor(l);
I am getting an error since any one of the row contains value larger than the column.
How to handle exception handling for those rows which is having errors. I would like insert the records which is having
no errors. Like SAVE EXCEPTIONS for 'forall' is there any option is available to handle exceptional records.
Please help.
Thanks & Regards,
Hari.Hari,
What's oracle version? Are you looking for something similar to this? see following example
DECLARE
TYPE array
IS
TABLE OF my_objects%ROWTYPE
INDEX BY BINARY_INTEGER;
data array;
errors NUMBER;
dml_errors exception;
error_count NUMBER := 0;
PRAGMA EXCEPTION_INIT (dml_errors, -24381);
CURSOR mycur
IS
SELECT *
FROM t;
BEGIN
OPEN mycur;
LOOP
FETCH mycur BULK COLLECT INTO data LIMIT 100;
BEGIN
FORALL i IN 1 .. data.COUNT
SAVE EXCEPTIONS
INSERT INTO my_new_objects
VALUES data (i);
EXCEPTION
WHEN dml_errors
THEN
errors := sql%BULK_EXCEPTIONS.COUNT;
error_count := error_count + errors;
FOR i IN 1 .. errors
LOOP
DBMS_OUTPUT.put_line( 'Error occurred during iteration '
|| sql%BULK_EXCEPTIONS(i).ERROR_INDEX
|| ' Oracle error is '
|| sql%BULK_EXCEPTIONS(i).ERROR_CODE);
END LOOP;
END;
EXIT WHEN c%NOTFOUND;
END LOOP;
CLOSE mycur;
DBMS_OUTPUT.put_line (error_count || ' total errors');
END;Regards
OrionNet -
Problem using DBMS_SQL in a form
hi,
I am trying to write a procedure which get an input string containing a SQL statement (a SELECT), executes it, fetches the rows and writes them into a Excel sheet. I found this article http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_sql.htm describing how to use DBMS_SQL package. My goal is to parse my query and store result in an array (using DEFINE_ARRAY).
I put theese two lines
(declare c number;)
c := dbms_sql.open_cursor;
dbms_sql.parse(c,p_query,dbms_sql.native); (p_query is my input string containing SQL)
But when I compile my program unit I get an error looking like that: "implementation restriction: DBMS_SQL.NATIVE: unable to access a remote package variable or cursor".
Where is the error? should I set something to access the package, or is it not possible to use it inside a form?W1zard is correct. Switch to Exec_SQL. Oracle does not support Forms using DBMS_SQL.
Now... please explain why you are not using a dynamic record group. It is SOOO much easier!
And if you want to look at some code using both methods, Exec_SQL and dynamic record group, you can download my "Quick Access" dynamic utility form, here: Oracle Forms Utilities
Maybe you are looking for
-
Creative Cloud Packager 1.2 problem
One of my customers created a package with Creative Cloud Packager 1.2. And he tries to install it, but the installation stops halfway and starts rolling back. Here is the PDApp.log 07/24/13 11:26:12:660 | [INFO] | | ASU | PIM | PIM | | | 8396 | B
-
BADI or User exit for FB50 to update custom field in table COEP
Hi all, i have added one custom field in table COEP through include CI_COBL . when i do posting through sales order(VA02) or purchase order(ME22n) that custom fields in COEP get updated with controlling document and through BADI AC_DOCUMENT i m updat
-
I don't know how to watch iTunes Trailers in iTunes on the iPad?!
Hi there! I'm trying to watch the iTunes movie trailers directly in iTunes on the iPad2... Is it even possible? And if it is, how do I watch them??
-
Can I install Original Mac OS X Snow Leopard on my PC/laptop?
Same as my topic~
-
Questions on importing pictures onto 3G from iPhoto: HOW?
I have a number of pictures I would like to import from a folder I have created in iPhoto 9.2 onto my iPhone 3Gs. I have w few questions: 1. These photos are both RAW and JPG and are between 8MP and 12MP in size. Will they be converted automatically