Using DBMS_SQL in a procedure
Hi,
I am using Oracle8i 8.1.5 on RedHat Linux 6.1. I have a stored procedure that executes some DDL statements using the DBMS_SQL package.
When I go to execute the stored procedure, I get the following errors:
ORA-01031: insufficient privileges
ORA-06512: at "SYS.DBMS_SYS_SQL", line 487
ORA-06512: at "SYS.DBMS_SQL", line 32
The user I am running the procedure as (who also happens to own the procedure) has DBA privileges as well as EXECUTE ANY PROCEDURE privilege.
Has anyone else seen this before, and what do I need to do to remedy it.
Timothy Sim
[email protected]
null
Who owns the stored procedure? In Oracle 7.X - 8.0, the person who owns the procedure is the person who executes it. In other words, Oracle assumes the identity of the owner of the procedure when it is executed. Therefor, it would not matter if you are a DBA, if the owner of the procedure is not a DBA. In Oracle 8.i, the default behavior is the same. However, you can change this behavior if you'd like (Make the executor the person who executes the DDL). I am not sure of the exact syntax, but I could get it for you if you need it.
This may be your problem.
-Jim Wartnick
[email protected]
Similar Messages
-
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 -
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 -
Create Table using DBMS_SQL package and size not exceeding 64K
I have a size contraint that my SQL size should not exceed 64K.
Now I would appriciate if some one could tell me how to create a table using
Dynamic sql along with usage of DBMS_SQL package.
Brief Scenario: Users at my site are not given permission to create table.
I need to write a procedure which the users could use to create a table .ALso my SQL size should not exceed 64K. Once this Procedure is created using DBMS_SQL package ,user should pass the table name to create a table.
Thanks/"If a user doesn't have permission to create a table then how do you expect they will be able to do this"
Well, it depends on what you want to do. I could write a stored proc that creates a table in my schema and give some other user execute privilege on it. They would then be able to create a able in my schema without any explicitly granted create table privilege.
Similarly, assuming I have CREATE ANY TABLE granted directly to me, I could write a stroe proc that would create a table in another users schema. As long as they have quota on their default tablespace, they do not need CREATE TABLE privileges.
SQL> CREATE USER a IDENTIFIED BY a
2 DEFAULT TABLESPACE users TEMPORARY TABLESPACE temp;
User created.
SQL> GRANT CREATE SESSION TO a;
Grant succeeded.
SQL> CREATE TABLE a.t (id NUMBER, descr VARCHAR2(10));
CREATE TABLE a.t (id NUMBER, descr VARCHAR2(10))
ERROR at line 1:
ORA-01950: no privileges on tablespace 'USERS'So, give them quota on the tablespace and try again
SQL> ALTER USER a QUOTA UNLIMITED ON users;
User altered.
SQL> CREATE TABLE a.t (id NUMBER, descr VARCHAR2(10));
Table created.Now lets see if it really belongs to a:
SQL> connect a/a
Connected.
SQL> SELECT table_name FROM user_tables;
TABLE_NAME
T
SQL> INSERT INTO t VALUES (1, 'One');
1 row created.Yes, it definitely belongs to a. Just to show that ther is nothing up my sleeve:
SQL> create table t1 (id NUMBER, descr VARCHAR2(10));
create table t1 (id NUMBER, descr VARCHAR2(10))
ERROR at line 1:
ORA-01031: insufficient privilegesI can almost, but not quite, see a rationale for the second case if you want to enforce some sort of naming or location standards but the whole thing seems odd to me.
Users cannot create tables, so lets give them a procedure to create tables?
John -
Dynamic SQL using DBMS_SQL Package
Hi,
How do i construct a
"select * from :table_name(input parameter)"
in a store proc in Oracle8 ?
I know this is only possible using DBMS_SQL package in Oracle 8.
I know that this is easily done using native Dyanamic SQL 8i onwards.
Also I want to return the resultset obtained above through a REF cursor.
Please Help !!!
Will be greatly indebted to anyone who can supply the code or direct me to some helpful information on the net.
Thanks in advance.
PeeyushYou are asking for two things here, use the tablename dynamically
as bind variable to fetch the resultset and send the resultset to
a stored procedure.
I thought of doing both of them with REF Cursors, no Dynamic SQL here.
Consider the following test,SQL> create or replace package ref_types as
2 type r_cursor(pSal NUMBER) is ref cursor;
3 end;
4 /
Package created.
-- Please note that I did some quick and dirty coding to switch
-- between the table names to show the results.
SQL> create or replace procedure p_dynamic_table
2 (pRefCur IN ref_types.r_cursor, pTable varchar2) as
3 emp_rec my_emp%ROWTYPE;
4 inv_rec test444%ROWTYPE;
5 begin
6 loop
7 IF UPPER(pTable) = 'MY_EMP' Then
8 fetch pRefcur into emp_rec;
9 Elsif UPPER(pTable) = 'INV' THEN
10 fetch pRefCur into inv_rec;
11 Else
12 exit;
13 End if;
14 exit when pRefcur%NOTFOUND;
15 If UPPER(pTable) = 'MY_EMP' Then
16 dbms_output.put_line(emp_rec.empno);
17 Elsif UPPER(pTable) = 'INV' Then
18 dbms_output.put_line(inv_rec.seq);
19 Else
20 Null;
21 End if;
22 end loop;
23 end;
24 /
Procedure created.
SQL> declare
2 vRefCur ref_types.r_cursor;
3 vTableName Varchar2(30);
4 begin
5 vTableName := 'my_emp';
6 open vRefCur for 'select * from ' || vTableName;
7 p_dynamic_table(vRefCur, vTableName);
8 close vRefCur;
9 vTableName := 'inv';
10 open vRefCur for 'select * from ' || vTableName;
11 p_dynamic_table(vRefCur, vTableName);
12 close vRefCur;
13 end;
14 /
7369
7499
7521
7566
7654
7698
7782
7788
7839
7844
7876
7900
7902
7934
1
2
3
4
5
6
7
8
9
10
PL/SQL procedure successfully completed.Thx,
Sri -
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 -
Using differing numbers of inputs for a similar query using DBMS_SQL
Hi,
I'm trying to use DBMS_SQL for a query that's either going to include the following WHERE statement:
"AND sched_requirement_id = pin_sr_id"
or not base upon conditions passed to the procedure calling it
v_sql_stmt :=
'SELECT COUNT(1)
INTO v_exists
FROM SCHED_PSA_REQS
WHERE sched_week_id = pin_sched_week_id
AND sequence_number = pin_seq_no
AND sched_original_req_id = pin_sor_id
----> AND sched_requirement_id = pin_sr_id<---------
AND ROWNUM = 1';
The only reason I can't use NDS for this is because of the pin_sr_id input doesn't exist inside the universe of the v_sql_stmt. If it was an inner join or something I could get away with it.
I read that DBMS_SQL doesn't care how many input values it gets until the dbms_sql.EXECUTE statement, but in all the documentation I've read through I have yet to come across an example that deals with a varying number of input values. Does anyone out there have an example of DBMS_SQL that deals with a varying number of bind input values based upon a conditional statement or input values?
Thanks!Mike,
Thanks for your input but the EXECUTE IMMEDIATE I would be using contains the following input values:
EXECUTE IMMEDIATE v_sql_stmt
INTO v_psa_req_id USING pin_sched_week_id, pin_seq_no, pin_sor_id, pin_sr_id;
I experimented with exactly what you suggested with scott/tiger:
set serveroutput on
DECLARE
v_target_val NUMBER;
v_str varchar2(4000);
f_tbl varchar2(200);
f_exp varchar2(200);
dept_var NUMBER:= 20;
BEGIN
f_tbl := ', dept d';
f_exp := 'and e.deptno = dept_var ';
--f_exp := ' and e.deptno = d.deptno ';
v_str := ' Select '
||' sal '
||' from emp e '
||f_tbl
||'
where '
||' e.empno = :b_v_for_where_cond '
||f_exp;
dbms_output.put_line(v_str);
EXECUTE IMMEDIATE v_str
INTO v_target_val USING 7369;
dbms_output.put_line(v_target_val);
EXCEPTION
when no_data_found then
dbms_output.put_line('No data exception raised');
when others then
dbms_output.put_line(SQLCODE||' '||SQLERRM);
END;
The code bombs miserably when I try to introduce a variable that's not fed in from the USING clause. From what I've learned of NDS all values must either be deliberately fed in like from the USING statement or must exist "inside the universe" of that SQL statement. Example if I use the line:
f_exp := ' and e.deptno = d.deptno ';
instead it works because d.deptno can be referenced from within that SQL statement from table dept.
So in my case either pin_sr_id is part of the USING clause or it's not. This is the one thing that DBMS_SQL still has over NDS is the fact "It does not need to know the number and types of arguments it will receive and process." (1) Oracle Database 10g PL/SQL Programming pg. 609.
They say you can use DESCRIBE_COLUMNS and DESCRIBE_COLUMNS2. However, I've yet to find an example of its use. If anybody has any web links to such it would be greatly appreciated. -
Can we use return statement in procedure?
Can we use return statement in procedure or we can use more than one return statement in procedure?
HamidHelal wrote:
NOReally? Did you at least test it? You can use RETURN in procedure or in anonymous PL/SQL block. The only restriction is you can't specify return value:
SQL> begin
2 dbms_output.put_line('Before return');
3 return;
4 dbms_output.put_line('After return');
5 end;
6 /
Before return
PL/SQL procedure successfully completed.
SQL> create or replace
2 procedure p1
3 is
4 begin
5 dbms_output.put_line('Before return');
6 return;
7 dbms_output.put_line('After return');
8 end;
9 /
Procedure created.
SQL> exec p1;
Before return
PL/SQL procedure successfully completed.
SQL> begin
2 dbms_output.put_line('Before return');
3 return 99;
4 dbms_output.put_line('After return');
5 end;
6 /
return 99;
ERROR at line 3:
ORA-06550: line 3, column 5:
PLS-00372: In a procedure, RETURN statement cannot contain an expression
ORA-06550: line 3, column 5:
PL/SQL: Statement ignored
SQL> create or replace
2 procedure p1
3 is
4 begin
5 dbms_output.put_line('Before return');
6 return 99;
7 dbms_output.put_line('After return');
8 end;
9 /
Warning: Procedure created with compilation errors.
SQL> show err
Errors for PROCEDURE P1:
LINE/COL ERROR
5/9 PL/SQL: Statement ignored
5/9 PLS-00372: In a procedure, RETURN statement cannot contain an
expression
SQL> SY. -
Tax calculation in CRM using R/3 pricing procedure
Hello Experts
We are using R/3 pricing procedure to calculate list prices in CRM.
The condition record for Taxes are maintained for condition type UTXJ in SD which is calling for tax procedure TAXUSJ and condition type JR1.
The records are maintained in FTXP. This is the SAP Standard way of maintaining taxes if external tax software is not used.
We have downloaded the condition records and condition types in CRM. The tables have all the required records downloaded from R/3.
When creating an order in Webshop the tax is not getting calculated.
The pricing analysis shows that the condition record for UTXJ condtion type is met, but it is not showing the tax maintained for condition type JR1.
Have we missed any settings here?
If we are using R/3 billling does the TTE needs to be activated for calculating taxes in the webshop?
If TTE needed to be used for calculating taxes, what are the settings needed to be configured?
Thanks in advance for the valuable advice.
SastriHi Sastri,
I am facing this problem with TAX Condition Records: Pricing error: Mandatory condition MWST is missing. I am using a R/3 pricing procedure correctly downloaded to CRM.
You say that you have downloaded the condition records and condition types in CRM. SAP Library says 'Prerrequisites: You have downloaded the tax condition records from SAP ECC'.
I don't know If my problem is caused because I haven't downloaded any Tax Condition Records. The fact is that I cannot determine which one is the Condition Object to download in R3AS.
I would really appreciate if you or anyone could tell me which object you did use.
thanks in advance,
Pablo
Edited by: Pablo Rodríguez Mateos on Sep 30, 2009 12:30 PM
Edited by: Pablo Rodríguez Mateos on Sep 30, 2009 8:59 PM
Solved! -
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 -
Hi,
I've a package with 6 procedures.
In one procedure I need to load the data into a flat file. The procedure contains only one select statement.
example:
procedure pro_test as
select * from emp;
end pro_test;
Shell script is being used to call one procedure inside the package and that procedure is calling remaining procedures inside it.
connected to database using sqlplus.
Can anyone help me out how I can spool the select statement inside the procedure. Version of oracle is 11g.Hi,
Why do you want to do
SELECT *
FROM table_name;
inside the procedure? Why not have your script run the procedure (without the query above), and then do that query afterwards, when you can use SPOOL to put the results in a file?
If you really must write a file from a procedure, here's an example of how to use utl_file.
First, SYSTEM (or some other user with the CREATE ANY DIRECCTORY system privilege) must create a directory, and grant privileges to use it. For example:
CREATE OR RPLEACE DIRECTORY a_dir AS 'C:\Foo\A_Folder';
GRANT ALL ON DIRECTORY a_dir TO PUBLIC;
Typically, SYSTEM does this once, and other people use it hundreds of times.
You might create a file in that directory like this:
CREATE OR REPLACE PROCEDURE write_dept
IS
f utl_file.file_type;
BEGIN
f := utl_file.fopen ('A_DIR', 'Dept.txt', 'W');
FOR r IN ( SELECT *
FROM scott.dept
LOOP
utl_file.put_line ( f
, TO_CHAR (r.deptno, 'FM00')
|| ' '
|| RPAD (r.dname, 15)
|| r.loc
END LOOP;
utl_file.fclose (f);
END write_dept;
SHOW ERRORS -
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; -
Need to execute Long Insert (6000 characters)statement using dbms_sql.parse
I built an insert statement which is more than 6000 (six thousand) characters stored into two varchar2 variables. I need to execute the statement using dbms_sql.parse.
I tryed using dbms_sql.varchar2s variable and having no success.
Any ideas or examples?
Thanks
JK6000 chars shouldn't be a problem.
What do you mean with no success?Did you get any error?
Any ideas or examples?See this thread
Re: execute immediate with a string longer than 32767 -
Using dynamic value in procedure on column
I want a PL/sql procedure to select the data from the column dynamically into a variable .
My friend suggested its not possible in PL/sql ,its only possible in sql .
What i want is when i execute the PL/sql procedure the a_1 must get the value of the data in the table,i do not want to use execute immediate in pl/sql.
I create a table A
create table A (a1 number,a2 number,a3 number);
insert into a values (1,2,3);
then i create a procedure
create or replace
procedure test_a (var IN number)
as
a_1 number;
i number;
z varchar2(5);
begin
if var=1
then
i:=1;
elsif var = 2
then
i:=2;
z:='A2';
end if;
SELECT 'a'||i into a_1 from a;
end;
Edited by: user536416 on May 13, 2010 4:03 AMI prefer reference cursors over execute immediate if I must do dynamic SQL (and have learned not to do it unless necessary - among other things dynamic SQL is hard to debug and maintain).
The advantages to ref cursors are
* execute immedate will only retrieve one row, ref cursors as many as you want
* easier for me to build the query first, then execute it (though you can do this with execute immediate too) for debugging
You can buiid the query dynamcially but have to be careful to make sure the select list matches the INTO clause on the fetch statements. Although the queries in the example below are hard coded I could have done something like
v_command_c := 'select '||var1||', '||var2||' from '''||table_name||'''';
open refcur for v_command_c;To randomize your column selection use something like the above with IF logic and a randomizer, possibly the DBMS_RANDOM package or a time extraction.
Something like
SET SCAN OFF
SET SERVEROUTPUT ON
DECLARE
--define composite data types
--reference cursor type - structure
TYPE RefCursorType IS REF CURSOR;
--collection (index-by table) type - structure
TYPE TablesTableType IS TABLE OF user_tables.table_name%TYPE
INDEX BY BINARY_INTEGER;
--define ref cursors to be used in BEGIN section
UserTablesCursor RefCursorType;
UserViewCursor RefCursorType;
--define actual index-by tables to be used in BEGIN section
NamesTable TablesTableType;
ViewsTable TablesTableType;
--define internal procedures to be used in this script
PROCEDURE ListItems(RefCursor IN RefCursorType,
DataTable IN OUT TablesTableType
) IS
Counter INTEGER := 1;
TableName user_tables.table_name%TYPE;
BEGIN
FETCH RefCursor INTO DataTable(Counter);
Counter := Counter + 1;
WHILE RefCursor%FOUND LOOP
FETCH RefCursor INTO DataTable(Counter);
Counter := Counter + 1;
END LOOP;
END; --ListItems;
PROCEDURE WriteItems(DataTable IN TablesTableType) IS
BEGIN
FOR i IN 1..DataTable.count LOOP
dbms_output.put_line(DataTable(i));
END LOOP;
END; --WriteItems;
BEGIN
OPEN UserTablesCursor FOR
SELECT table_name
FROM user_tables;
ListItems(UserTablesCursor,NamesTable);
CLOSE UserTablesCursor;
dbms_output.put_line('user_tables');
dbms_output.put_line('--------------');
WriteItems(NamesTable);
--second defintion & set of calls
dbms_output.put_Line(CHR(13));
OPEN UserTablesCursor FOR
SELECT view_name
FROM user_views;
ListItems(UserTablesCursor,ViewsTable);
CLOSE UserTablesCursor;
dbms_output.put_line('user_views');
dbms_output.put_line('--------------');
WriteItems(ViewsTable);
END;
/Edited by: riedelme on May 13, 2010 5:44 AM -
Subreport using the same Stored Procedure
I have a main report and a subreport that uses the same stored procedure. The stored procedure has 12 parameters and what I want to avoid is the user having to enter the same parameter values twice. When I created the subreport I put in the parameters + one field, the employee id field so that I could just test it on one employee.
After I entered the subreport into the main report, when I got to refresh the data, it's asking for params for BOTH reports. Is there any way to get around this? Because of the way the stored procedure returns the data, I had to create a subreport instead of just fitting it into one of the footers. I've looked at the subreport link and added the employee id as the key. When I ran it the first time, it returned the wrong date range on the sub.
Help! this thing is Way passed due!!Right click on the sub report and choose change sub report links
Top left pane contains your main report parameters use > to add
In the bottom left corner of that dialog then scroll down until you see the corresponding sub report parameter
Repeat for each parameter
This should then take the main report params and pass into the sub report
Maybe you are looking for
-
Error when connecting to socket
OK, ive got tomcat 5 up and running and have created a servlet from an example i found on the internet and so to my knowledge it should work. When trying to open a socket on the ip 127.0.0.1:2083 i get the error ... javax.microedition.io.ConnectionNo
-
Discoverer 3.1 Turorial PDF
I am trying to get the PDF version of the Discoverer 31 enduser tutorial. Does anyone know where I can get it. The only ones that are online are the 4.1 and greater. Any help would be greatly appreciated.
-
When I execute my applet, it gives me this error: Exception in thread "main" java.lang.NoSuchMethodError: main However when it runs in the appletviewer fine. Heres the code: import java.applet.Applet; import java.awt.*; import javax.swing.*; public c
-
I have created a template and DW CS4 has automatically placed the page title in the non-editable region of the document. When I create any pages using the template it will not allow me to rename them. How do I fix this? Chris
-
How to make a stroke part of the shape
One of the letters (S) on a logo I am working on doesn't look the right thickness alongside the other letters - there isnt a bolder version available so I have added a small stroke to make it visually balanced (text is converted to outlines). I would