Sort of dynamic SQL
Ok here is a question.
I seem to remember a method of dynamically building different SQL statements.
Specifically this is what I want to do with Update Statistics via ISQL.
This technique has many other applications for inserts and updates as well. I only use this as an example.
1. run a script on a system table which would return all the user tables in a given DB.
2. That list would be written to a text file.
3. Another script would read the text file and build "update all statistics tablename" and writes this to another text file.
4. Another script then opens that text file and runs it therefore updating statistics on all tables with me have to change it every time a table was dropped or added
This technique has many other applications for inserts and updates as well.
Do you know anything about this? I am going to post this on the Sybase site as well. If I get anything I will pass it on.
Thanks,
Bob
P.s. it goes something like this...
-----------------BUILD SQL script--------------------- select
'update ' || f.table_name || ' set (' ||f.column_name ||') to
'||''''||f.new_value ||''''|| ' where '||f.column_name ||' = '||
'||f.old_value ||''''||'; go' from temp_results_table f;
You seem to have virtually answered your own question. Which bit are you having trouble with ?
Similar Messages
-
Dynamic sql in a trigger ...please help
HI All,
I've got a really large table called 'shop' of the form
id number, product1_cost number, product2_cost number,product3_cost, product4_cost....and so on until product20_cost,
product1_desc, product2_desc....and so so on until..product20_desc, active, start_date, end_date, usercreated,
useramended.
I have now created a new application. The new table is called 'products' which has an id, cost, description, oldproductid, shopid
I need a trigger on the table shop so that if anyone uses the old application, it automatically creates the product rows in my new table 'products'
Not all the product fields on the 'shop' table have data so I only want to migrate data if necessary.
The trigger would do the following:
1. Loop through all the products, if the product was updated and product number does not exist on 'products.oldproductid' then create a new record in products
2. If product was updated and product number does exist then simply update the product details.
I have got the trigger to work with product1. However, I'm finding it difficult to write some sort of dynamic sql that would
do the same check on all 20 products without having to repeat the code 20 times.
can someone help?
CREATE OR REPLACE TRIGGER TR_PRODCOST
AFTER UPDATE ON SHOP
FOR EACH ROW
DECLARE
v_oldproductid products.oldpackageid%TYPE;
v_datedl products.datedl%TYPE;
v_product_sq number;
v_status number := 1;
v_cost number(11, 2);
v_old_pkg number:= :old.product1_cost;
v_new_pkg number:= :new.product1_cost;
v_old_desc varchar2(100):= :old.product1_desc;
v_new_desc varchar2(100):= :new.product1_desc;
v_old_shop varchar2(10):= :old.shopid;
v_new_shop varchar2(10):= :new.shopid;
CURSOR get_newproduct_cur IS
SELECT p.oldproductid, p.datedl
FROM products p
WHERE p.shopid = v_new_shop
AND p.oldproductid = 1;
BEGIN
IF nvl(v_new_pkg, -1) <> nvl(v_old_pkg, -1) THEN
OPEN get_newproduct_cur;
FETCH get_newproduct_cur
INTO v_oldproductid, v_datedl;
IF get_newproduct_cur%NOTFOUND THEN
v_oldproductid := null;
v_datedl := null;
END IF;
CLOSE get_newproduct_cur;
IF v_oldproductid is null THEN
SELECT SEQ_products.nextval INTO v_product_sq FROM dual;
INSERT into products p
(product_ID,
SHOPID,
DESCRIPTION,
COST,
DATECR,
USERCR,
OLDproductID,
SHORT_DESCRIPTION)
VALUES
(v_product_sq,
v_new_shop,
v_new_desc,
v_new_pkg,
sysdate,
'OLFORMS',
1,
v_new_desc);
ELSE
IF v_new_pkg is null THEN
UPDATE products p
set p.datedl = sysdate,
p.cost = 0.00
WHERE p.shopid = v_new_shop
AND p.oldproductid = 1;
ELSE
UPDATE products p
set p.cost = v_new_pkg,
p.datedl = null,
WHERE p.shopid = v_new_shop
AND p.oldproductid = 1;
END IF;
IF v_new_desc = v_old_desc THEN
v_status := 1;
ELSE
UPDATE products p
SET p.description = v_new_desc
WHERE p.shopid = v_new_shop
AND p.oldproductid = 1;
END IF;
END IF;
ELSE
END IF;
END IF;
END;Hi All,
Thanks about your advice on the relational design. However, this is only a temporary fix. The old shop table was created about 10 years ago by someone else. Our users have to use the old pages for about 2-3 weeks and therefore I need a quick fix that will create the products on the new tables.
The other problem I have is that another powerbuilder based applicated also uses the same tables and that application is being modified to use the new tables but not until another 3 weeks. To ensure both systems are available in parallel, I have to implement this temporary solution.
B_Binoy, you seem you have a solution there but I didn't quite get it. I tried putting the whole pl/sql block in a string, replacing the '1' in product 1 using a variable x and then using execute immediate but because of the :new and :old bind variables, it kept giving me errors like 'variables not bound' etc. If you don't mind, please can you expand on your solution?
Many Thanks -
We are running an oracle sql procedure that uses a LOT of dynamic sql. We are using a 3rd party package (SQR) as a sort of shell to run the sql procedure. The 3rd party package passes to us an oracle error. This error says, in effect, that there are no inactive database cursors available and that the sql program is too large to process. We conclude from this that we must increase one or more of the cursor parameters in init.ora (v$parameters). Is this the correct assumption? If not, does anyone know what we can do? We'd prefer not to break up the sql procedure into smaller pieces.
increase the parameter for open cursors.
check, wether all cursors in your programs are closed in time, or if you are using ref cursors from front-ends (e.g. Java JDBC) that this front-ends close these ref cursors , too.
If you want to decrease the size of procedures get rid of comments, superfluos spaces, tabs, etc.
keep a commented version outside vor documentation purposes.
Hope thsi helps -
Dynamic SQL within a SQL Query ?
is there any possibility to do like this ?
SELECT table_name, XXXXXXXX('SELECT Count(*) FROM '||table_name) tot_rows
FROM dba_tables
WHERE owner = 'SCOTT';or any other trick to run dynamic SQL within the SQL Query?
Hoping....that it should be.
Regards,
OrapdevOne small disadvantage: it is executing 202 SQL statements: 3 "user SQL statements" (the one above and the 2 "select count(*)..."), and 199 internal ones ...How did you get to those numbers?
I just traced this statement and found completely different results:
TKPROF: Release 10.2.0.3.0 - Production on Tue Jul 10 12:12:10 2007
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Trace file: diesl10r2_ora_5440.trc
Sort options: default
count = number of times OCI procedure was executed
cpu = cpu time in seconds executing
elapsed = elapsed time in seconds executing
disk = number of physical reads of buffers from disk
query = number of buffers gotten for consistent read
current = number of buffers gotten in current mode (usually for update)
rows = number of rows processed by the fetch or execute call
declare cursor NlsParamsCursor is SELECT * FROM
nls_session_parameters;begin SELECT Nvl(Lengthb(Chr(65536)),
Nvl(Lengthb(Chr(256)), 1)) INTO :CharLength FROM dual; for NlsRecord in
NlsParamsCursor loop if NlsRecord.parameter = 'NLS_DATE_LANGUAGE' then
:NlsDateLanguage := NlsRecord.value; elsif NlsRecord.parameter =
'NLS_DATE_FORMAT' then :NlsDateFormat := NlsRecord.value; elsif
NlsRecord.parameter = 'NLS_NUMERIC_CHARACTERS' then
:NlsNumericCharacters := NlsRecord.value; elsif NlsRecord.parameter =
'NLS_TIMESTAMP_FORMAT' then :NlsTimeStampFormat := NlsRecord.value;
elsif NlsRecord.parameter = 'NLS_TIMESTAMP_TZ_FORMAT' then
:NlsTimeStampTZFormat := NlsRecord.value; end if; end loop;end;
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 1
Fetch 0 0.00 0.00 0 0 0 0
total 2 0.00 0.00 0 0 0 1
Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 50
SELECT NVL(LENGTHB(CHR(65536)), NVL(LENGTHB(CHR(256)), 1))
FROM
DUAL
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.01 0.00 0 0 0 0
Fetch 1 0.00 0.00 0 0 0 1
total 3 0.01 0.00 0 0 0 1
Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 50 (recursive depth: 1)
Rows Row Source Operation
1 FAST DUAL (cr=0 pr=0 pw=0 time=7 us)
SELECT *
FROM
NLS_SESSION_PARAMETERS
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 1 0.00 0.00 0 0 0 17
total 3 0.00 0.00 0 0 0 17
Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 50 (recursive depth: 1)
Rows Row Source Operation
17 FIXED TABLE FULL X$NLS_PARAMETERS (cr=0 pr=0 pw=0 time=124 us)
select PARAMETER,VALUE
from
nls_session_parameters where PARAMETER in('NLS_NUMERIC_CHARACTERS',
'NLS_DATE_FORMAT','NLS_CURRENCY')
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 1 0.00 0.00 0 0 0 3
total 3 0.00 0.00 0 0 0 3
Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 50
Rows Row Source Operation
3 FIXED TABLE FULL X$NLS_PARAMETERS (cr=0 pr=0 pw=0 time=57 us)
select to_char(9,'9C')
from
dual
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 1 0.00 0.00 0 0 0 1
total 3 0.00 0.00 0 0 0 1
Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 50
Rows Row Source Operation
1 FAST DUAL (cr=0 pr=0 pw=0 time=2 us)
SELECT table_name,
DBMS_XMLGEN.getxmltype ('select count(*) c from ' || table_name).EXTRACT
('//text').getstringval
() tot
FROM dba_tables
WHERE table_name IN ('EMP', 'DEPT') AND owner = 'SCOTT'
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 1 0.01 0.02 0 48 0 2
total 3 0.01 0.02 0 48 0 2
Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 50
Rows Row Source Operation
2 HASH JOIN (cr=42 pr=0 pw=0 time=2952 us)
2 MERGE JOIN CARTESIAN (cr=42 pr=0 pw=0 time=1206 us)
2 NESTED LOOPS OUTER (cr=42 pr=0 pw=0 time=478 us)
2 NESTED LOOPS OUTER (cr=36 pr=0 pw=0 time=421 us)
2 NESTED LOOPS OUTER (cr=30 pr=0 pw=0 time=379 us)
2 NESTED LOOPS OUTER (cr=30 pr=0 pw=0 time=365 us)
2 NESTED LOOPS (cr=22 pr=0 pw=0 time=312 us)
2 NESTED LOOPS (cr=16 pr=0 pw=0 time=272 us)
2 NESTED LOOPS (cr=8 pr=0 pw=0 time=172 us)
1 TABLE ACCESS BY INDEX ROWID USER$ (cr=2 pr=0 pw=0 time=56 us)
1 INDEX UNIQUE SCAN I_USER1 (cr=1 pr=0 pw=0 time=30 us)(object id 44)
2 INLIST ITERATOR (cr=6 pr=0 pw=0 time=111 us)
2 TABLE ACCESS BY INDEX ROWID OBJ$ (cr=6 pr=0 pw=0 time=87 us)
2 INDEX RANGE SCAN I_OBJ2 (cr=4 pr=0 pw=0 time=54 us)(object id 37)
2 TABLE ACCESS CLUSTER TAB$ (cr=8 pr=0 pw=0 time=98 us)
2 INDEX UNIQUE SCAN I_OBJ# (cr=4 pr=0 pw=0 time=26 us)(object id 3)
2 TABLE ACCESS CLUSTER TS$ (cr=6 pr=0 pw=0 time=39 us)
2 INDEX UNIQUE SCAN I_TS# (cr=2 pr=0 pw=0 time=13 us)(object id 7)
2 TABLE ACCESS CLUSTER SEG$ (cr=8 pr=0 pw=0 time=37 us)
2 INDEX UNIQUE SCAN I_FILE#_BLOCK# (cr=4 pr=0 pw=0 time=21 us)(object id 9)
0 INDEX UNIQUE SCAN I_OBJ1 (cr=0 pr=0 pw=0 time=4 us)(object id 36)
2 TABLE ACCESS BY INDEX ROWID OBJ$ (cr=6 pr=0 pw=0 time=33 us)
2 INDEX UNIQUE SCAN I_OBJ1 (cr=4 pr=0 pw=0 time=23 us)(object id 36)
2 TABLE ACCESS CLUSTER USER$ (cr=6 pr=0 pw=0 time=28 us)
2 INDEX UNIQUE SCAN I_USER# (cr=2 pr=0 pw=0 time=12 us)(object id 11)
2 BUFFER SORT (cr=0 pr=0 pw=0 time=716 us)
1 FIXED TABLE FULL X$KSPPI (cr=0 pr=0 pw=0 time=661 us)
1436 FIXED TABLE FULL X$KSPPCV (cr=0 pr=0 pw=0 time=1449 us)
select count(*) c
from
EMP
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 0.00 0.00 0 1 0 1
total 4 0.00 0.00 0 1 0 1
Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 50 (recursive depth: 1)
Rows Row Source Operation
1 SORT AGGREGATE (cr=1 pr=0 pw=0 time=96 us)
14 INDEX FULL SCAN EMP_IDX (cr=1 pr=0 pw=0 time=46 us)(object id 61191)
select metadata
from
kopm$ where name='DB_FDO'
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 1 0.00 0.00 0 2 0 1
total 3 0.00 0.00 0 2 0 1
Misses in library cache during parse: 0
Optimizer mode: CHOOSE
Parsing user id: SYS (recursive depth: 1)
Rows Row Source Operation
1 TABLE ACCESS BY INDEX ROWID KOPM$ (cr=2 pr=0 pw=0 time=42 us)
1 INDEX UNIQUE SCAN I_KOPM1 (cr=1 pr=0 pw=0 time=22 us)(object id 365)
select count(*) c
from
DEPT
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 0.00 0.00 0 1 0 1
total 4 0.00 0.00 0 1 0 1
Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 50 (recursive depth: 1)
ALTER SESSION SET sql_trace=FALSE
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 0 0.00 0.00 0 0 0 0
total 2 0.00 0.00 0 0 0 0
Misses in library cache during parse: 0
Parsing user id: 50
OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS
call count cpu elapsed disk query current rows
Parse 5 0.00 0.00 0 0 0 0
Execute 5 0.00 0.00 0 0 0 1
Fetch 3 0.01 0.02 0 48 0 6
total 13 0.01 0.03 0 48 0 7
Misses in library cache during parse: 0
OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS
call count cpu elapsed disk query current rows
Parse 5 0.00 0.00 0 0 0 0
Execute 5 0.01 0.00 0 0 0 0
Fetch 7 0.00 0.00 0 4 0 21
total 17 0.01 0.00 0 4 0 21
Misses in library cache during parse: 0
9 user SQL statements in session.
1 internal SQL statements in session.
10 SQL statements in session.
Trace file: diesl10r2_ora_5440.trc
Trace file compatibility: 10.01.00
Sort options: default
1 session in tracefile.
9 user SQL statements in trace file.
1 internal SQL statements in trace file.
10 SQL statements in trace file.
10 unique SQL statements in trace file.
132 lines in trace file.
0 elapsed seconds in trace file.I only see a ratio of 1:9 for user- to internal SQL statements?
michaels> select * from v$version
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod
PL/SQL Release 10.2.0.3.0 - Production
CORE 10.2.0.3.0 Production
TNS for 32-bit Windows: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production -
Using dynamic sql in triggers with :OLD values
i need to record all deleted rows from an entire schema in a single table. for that matter i created a function that receives a table name and generate an insert command according to it's primary key columns. i call this function in the table triggers. in order to insert the old values before the delete i use :OLD with "execute immediate" as followed :
create or replace trigger trg_some_tbl_bd
before delete on some_tbl
for each row is
declare
v_sql varchar2(4000);
begin
v_sql := generate_insert_command('some_table');
execute immediate v_sql;
end;
the return value from "generate_insert_command" function is the string:
insert into deleted_table (table_name , date , pk1 , pk2) values
('some_table' , sysdate , :OLD.pk1 , :OLD.pk2)
the execute immediate command notice the :OLD and looks for bind variables.
i need to know i can i bypass that. i tried looking for escape characters but couldent find any...
i would appriciate any help , it's kynda urgent
Thanks !I don't believe this is going to work. Even if you could get around the fact that :old looks like a bind variable, the :old values are not visible to the dynamic SQL statement, they're like local variables in that respect.
If you wanted to pass old values in, those values would have to be passed in as bind variables, i.e.
EXECUTE IMMEDIATE v_sql USING :old.pk1, :old.pk2which defeats the purpose of using dynamic SQL.
Since you have to create a trigger for each table, I don't see why you would bother with dynamic SQL inside the trigger-- your table structure is fixed when the trigger is created. You could write dynamic SQL that generated the triggers in the first place, but the code inside the trigger should be dynamic.
As an aside, you realize that logging every audit record into a single table creates rather massive contention issues, right? And have you considered how painful it is to query this sort of table? Have you considered other options for maintaining history like Workspace Manager? Or at least separate history tables for each table?
Justin -
If table testrh2 has the following columns and data
col1 --> NULL
col2 --> 2
and table testrh has the following columsn and data
col1 --> NULL
How could I write a dynamic SQL statement to join on the nulls? I've written the following block as a starting point.
declare
cursor c1 is select col1 from isis.testrh;
lval varchar2(1000);
lval2 varchar2(1000);
begin
for r1 in c1 loop
lval := 'select col2 from isis.testrh2 where col1 = '||r1.col1;
execute immediate lval into lval2;
dbms_output.put_line(lval2);
end loop;
end;You can't compare null values with '=' in Oracle SQL.
Null can only be compared with <column> is null .
You can see it when you try these two queries:
select * from dual where null is null; -- you will see one row
select * from dual where null=null; -- you will see no rowsThat's why you have to write something like
(<column1>=<column1> or (<column1> is null and <column2> is null))This should also work with null:
decode(<column1>,<column2>,1,0)=1By the way, why do you use dynamic sql?
lval := 'select col2 from isis.testrh2 where col1 = '||r1.col1;
I think you could replace your two lines ( lval:= ... AND execute immediate) by this:
begin
select col2
into lval
from isis.testrh2
where decode(col1,r1.col1,1,0)=1;
dbms_output.put_line('lval='||lval);
exception
when no_data_found then
dbms_output.put_line('no data found'); -- or whatever you want
end;Edited by: hartmutm on 02.10.2010 23:54 -
How to rename C00n generic column names in a Dynamic SQL report
I have a an interface whereby the user can select 1 to 60 (upper limit) columns out of 90 possible columns (using a shuttle) from one table before running a report.
To construct the SQL that will eventually execute, I'm using a "PLSQL function body returning SQL query" with dynamic SQL. The only problem is that I have to use "Generic Column Names" option to be able to compile the code and end up with c001 to c060 as the column names.
How can I use the names of the selected columns as the report headings rather than c001, C002... etc?
I do not know beforehand which columns, or how many columns or the order of the selected columns.
I know Denes K has a demo called Pick Columns but I can't access his code. I have a hunch though that he may be just using conditions to hide/show the apropriate columns.
thanks in advance
PaulPHi Paul
I would change the Heading Type in the Report Details screen to PLSQL and use the shuttle item to return the column values. e.g.
RETURN :p1_shuttle;
I'm assuming the shuttle already has a colon separated list of the column headings in the correct order?
I hope that helps
Shunt -
hi all,
I am using oracle 10g version.
I have one table but i do not have any unique id to identify the row.
I want to have a unique id temporarily in my select statement (dynamic sql) in a stored procedure while fetching the results, so that i can return my results along with the unique id through the cursor.
Please help me...
Thanks in advance to all...it depends if you have a more than one column that you can uniquely identify and put them together by concatenating you can have a unique id. or a rowid might help you to temporarily identify a row.
SQL> select e.* from employee e;
FNAME MINIT LNAME SSN BDATE ADDRESS SEX SALARY SUPERSSN DNO
John B Smith 123456789 09-Jan-1965 731 fONDREN, hOUSTON, TX M 30000.00 333445555 5
Frankin T Wong 333445555 08-Dec-1955 683 Voss, Houston,Tx M 40000.00 888665555 5
Alicia J Zelaya 999887777 19-Jul-1968 3321Castle, Spring, TX F 25000.00 987654321 4
Jennifer S Wallace 987654321 20-Jun-2041 291 Berry, Bellaire, TX F 43000.00 888665555 4
Ramesh K Narayan 666884444 15-Sep-1962 975 Fire Oak, Humble, TX F 38000.00 333445555 5
Joyce A English 453453453 31-Jul-1972 5631 Rice,Houston,TX F 25000.00 333445555 5
Ahmad V Jabbar 987987987 29-Mar-1969 980 Dallas,Houston, TX M 25000.00 987654321 4
James E Borg 888665555 10-Nov-2037 450 Stone, Houston, TX M 55000.00 1
8 rows selected
SQL> select rowid, e.* from employee e;
ROWID FNAME MINIT LNAME SSN BDATE ADDRESS SEX SALARY SUPERSSN DNO
AAD8pbAAJAAAJ4fAAA John B Smith 123456789 09-Jan-1965 731 fONDREN, hOUSTON, TX M 30000.00 333445555 5
AAD8pbAAJAAAJ4fAAB Frankin T Wong 333445555 08-Dec-1955 683 Voss, Houston,Tx M 40000.00 888665555 5
AAD8pbAAJAAAJ4fAAC Alicia J Zelaya 999887777 19-Jul-1968 3321Castle, Spring, TX F 25000.00 987654321 4
AAD8pbAAJAAAJ4fAAD Jennifer S Wallace 987654321 20-Jun-2041 291 Berry, Bellaire, TX F 43000.00 888665555 4
AAD8pbAAJAAAJ4fAAE Ramesh K Narayan 666884444 15-Sep-1962 975 Fire Oak, Humble, TX F 38000.00 333445555 5
AAD8pbAAJAAAJ4fAAF Joyce A English 453453453 31-Jul-1972 5631 Rice,Houston,TX F 25000.00 333445555 5
AAD8pbAAJAAAJ4fAAG Ahmad V Jabbar 987987987 29-Mar-1969 980 Dallas,Houston, TX M 25000.00 987654321 4
AAD8pbAAJAAAJ4fAAH James E Borg 888665555 10-Nov-2037 450 Stone, Houston, TX M 55000.00 1
8 rows selected
SQL> -
Can we use Dynamic SQL in Oracle Reports ?
Hi ,
Can we use Dynamic SQL in Oracle Reports ?
If yes please give some examples .
Thanx
sriniI believe the built-in package SRW.Do_Sql is what you are looking for
Example from the document:
/* Suppose you want to create a "table of contents" by getting the
** first character of a columns value, and page number on which its
** field fires to print. Assume that you want to put the "table of
contents"
** into a table named SHIP. You could write the following construct:
DECLARE
PAGE_NO NUMBER;
PAGE_FOR INDEX NUMBER;
SORT_CHAR CHAR(1);
CMD_LINE CHAR(200);
BEGIN
SORT_CHAR := :SORT_NAME ;
IF :CALLED = Y THEN
SRW.GET_PAGE_NUM(PAGE_FOR_INDEX);
SRW.USER_EXIT(RWECOP PAGE_FOR_INDEX
P_START_PAGENO);
SRW.MESSAGE(2,TO_CHAR(:P_START_PAGENO));
END IF;
SRW.GET_PAGE_NUM(PAGE_NO);
CMD_LINE := INSERT INTO SHIP VALUES
(||SORT_CHAR||,||TO_CHAR(PAGE_NO)||);
SRW.MESSAGE(2,CMD_LINE);
SRW.DO_SQL(CMD_LINE);
COMMIT;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
NULL;
WHEN SRW.DO_SQL_FAILURE THEN
SRW.MESSAGE(1,FAILED TO INSERT ROW INTO SHIP TABLE);
WHEN OTHERS THEN
COMMIT;
END; -
How to mention numbers in array while creating dynamic sql
Hi,
I am creating a dynamic sql and in that I am selecting some rows from the table and storing it into the array.
I am firing this sql statement within trigger.
But i got the error:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
I took the same sql statement (the one which was created dynamically) in the procedure and tried to run it but it gave me the same error.
=========================================================
Begin
type NewValueArray is table of varchar2(4000);
nVal_Array NewValueArray;
Begin
Select
DISCOVERYHOSTNAME,
DISCOVERYMACPRIMARYPHYSICAL,
DISCOVERYSERIALNUMBER
into
nVal_Array(1),
nVal_Array(2),
nVal_Array(3)
from ALT_42_Consolidated
where rowid = 'AAEWNEABXAAAAkxAAA';
EXCEPTION
WHEN others THEN
Raise;
End;
=========================================================
I tried using to_number but still the same error.
Can some body please inform me where I am goofing.
Thanks!I got a different error, but you need to initialise your nval_array and make sure it can hold three values. The following should work:
declare
type NewValueArray is table of varchar2(4000);
nVal_Array NewValueArray := NewValueArray();
Begin
nval_array.extend(3);
Select col1, col2, col3
into nVal_Array(1), nVal_Array(2), nVal_Array(3)
from tab1
where rowid = 'AAAGJZAAGAAAAAWAAV';
EXCEPTION
WHEN others THEN
Raise;
End;
/ -
Hi all,
I'm under 10G r1
I want to execute a dynamic SQL with merge in it
when I try with insert ,update it works
but I just want to use merge if possible
this is the code
CREATE OR REPLACE
PROCEDURE I_COLUMN_CDS_FIXINGS
v_ticker VARCHAR2,
v_date DATE,
v_tenor VARCHAR2,
v_bid NUMBER,
v_ask NUMBER)
AS
l_column VARCHAR2(80);
v_column_bid VARCHAR2(80);
v_column_ask VARCHAR2(80);
v_source VARCHAR2(60) :='CMAN@BBG';
v_gen_order pls_integer;
BEGIN
SELECT 'CDS_SPREAD_'
||v_TENOR
||'_BID'
INTO v_column_bid
FROM dual;
SELECT 'CDS_SPREAD_'
||v_TENOR
||'_ASK'
INTO v_column_ask
FROM dual;
dbms_output.put_line(v_ticker||' '||v_column_ask||' '||v_column_bid||' '||v_bid|| ' '||v_ask||' '||v_date);
EXECUTE immediate 'MERGE into CDS_FIXINGS C
using ( select v_ticker,'
||v_column_bid
||','
||v_column_ask
||',trunc(v_date) info_date ,v_bid,v_ask from dual ) B
on (C.cds_ticker=b.v_ticker and c.info_date=b.info_date)
WHEN MATCHED THEN
update set '||v_column_bid||'=:1,'||v_column_ask||'=:2 using v_bid,v_ask
WHEN NOT MATCHED THEN
INSERT
CDS_TICKER ,
INFO_DATE ,
'||v_column_bid||' ,
'||v_column_ask||' ,
SOURCE
VALUES (:1, :2,:3,:4,:5) ' using v_ticker,
v_date ,
v_bid ,
v_ask ,
v_source
---EXCEPTION
---when others then raise;
end;
when I launch hte procedure
exec i_column_cds_fixings('TEST',trunc(sysdate-1),'8Y',4232.01,4234.02);
I get
ERROR at line 1:
ORA-00933: SQL command not properly ended
ORA-06512: at "OWNER_HISTO.I_COLUMN_CDS_FIXINGS", line 26
ORA-06512: at line 1
How can I deal with this
Thanks
babata
I getSorry
this is formatted one
REATE OR REPLACE PROCEDURE i_Column_cds_FixIngs
(v_Ticker VARCHAR2,
v_Date DATE,
v_Tenor VARCHAR2,
v_Bid NUMBER,
v_Ask NUMBER)
AS
l_Column VARCHAR2(80);
v_Column_Bid VARCHAR2(80);
v_Column_Ask VARCHAR2(80);
v_Source VARCHAR2(60) := 'CMAN@BBG';
v_gen_Order PLS_INTEGER;
BEGIN
SELECT 'CDS_SPREAD_'
||v_Tenor
||'_BID'
INTO v_Column_Bid
FROM Dual;
SELECT 'CDS_SPREAD_'
||v_Tenor
||'_ASK'
INTO v_Column_Ask
FROM Dual;
dbms_Output.Put_Line(v_Ticker
||' '
||v_Column_Ask
||' '
||v_Column_Bid
||' '
||v_Bid
||' '
||v_Ask
||' '
||v_Date);
EXECUTE IMMEDIATE 'MERGE into CDS_FIXINGS C
using ( select v_ticker,'
||v_Column_Bid
||','
||v_Column_Ask
||',trunc(v_date) info_date ,v_bid,v_ask from dual ) B
on (C.cds_ticker=b.v_ticker and c.info_date=b.info_date)
WHEN MATCHED THEN
update set '
||v_Column_Bid
||'=:1,'
||v_Column_Ask
||'=:2 USING v_bid,v_ask
WHEN NOT MATCHED THEN
INSERT
CDS_TICKER ,
INFO_DATE ,
||v_Column_Bid
||' ,
||v_Column_Ask
||' ,
SOURCE
VALUES
:1,
:2,
:3,
:4,
:5
USING v_Ticker,v_Date,v_Bid,v_Ask,v_Source;
---EXCEPTION
---when others then raise;
END;
/ -
Dynamic SQL and 255 bytes per line limit
I would like to create a Dynamic SQL statement that is longer than 255 bytes. What do I need to do to embed a carriage return or something so that the resultant string is broken up into multiple lines?
Please ignore. This was a stupid question!
-
Dynamic SQL and GRANT CREATE ANY TABLE
hi gurus,
i have a dynamic SQL in a procedure where a table will be created from an existing table without data.
strSQL:='create table ' || strTemp || ' as select * from ' || strArc || ' where 1=2';
execute immediate strSQL;
without GRANT CREATE ANY TABLE for the user, *"ORA-01031: insufficient privileges"* error during execution.
Is there a way to tackle this issue without providing GRANT CREATE ANY TABLE privilige?
many thanks,
Charlesravikumar.sv wrote:
The problem is not because of dynamic sql...It probably has something to do with dynamic SQL or, more accurately, dynamic SQL within a stored procedure.
From a SQL*Plus command prompt, you can create a table if your account has the CREATE TABLE privilege either granted directly to it or granted to a role that has been granted to your account. Most people probably have the CREATE TABLE privilege through a role (hopefully a custom "developer role" that has whatever privileges you grant to users that will own objects but potentially through the default RESOURCE role). That is not sufficient to create tables dynamically via a definer's rights stored procedure. Only privileges that are granted directly to the user, not those granted via a role, are visible in that case.
I expect that the DBAs are granting the CREATE ANY TABLE privilege directly to the account in question rather than through whatever role(s) are being used which is why that appears to solve the problem.
Justin -
Dynamic SQL and IN CLAUSE from Pro C code
Hi Guys,
Tyring to embed sql in Pro C. Here I don't know in hand how many items will be there in the IN Clause of my dynamic sql. Tried this with a loop and then adding actual values to the stement and then executing it. This worked but as this hard coding makes it literal sql and hence hampers performance. Can any one help me with how to put bind variables where we don't know how many of them will be there in a dynamic sql.
Thanks,Dynamic SQL supports user defined types, try passing a collection and using TABLE(CAST(collection)) in the SQL statement.
In the current approach (creating IN clause at runtime) keep in mind that in a IN clause you can put a maximum of 255 elements...
Max -
Dynamic SQL and use of aggregate functions
Hello Forum members,
I'm trying to create dynamic SQL in a function module and return the MAX value of a field.
I am passing in a parameter called CREATE_FIELD_NAME, and my SQL is
SELECT MAX(CREATE_FIELD_NAME) INTO (CREATE_DATE) FROM (TABLE_NAME).
I also tried:
SELECT MAX((CREATE_FIELD_NAME)) INTO (CREATE_DATE) FROM (TABLE_NAME).
But abap is not recognizing my variable as a variable in either case, but rather, as a field name.
Anyone know a way around this?
Thanks in advance,
Jeff
Here is my program:
FUNCTION ZJLSTEST4.
""Local Interface:
*" IMPORTING
*" VALUE(TABLE_NAME) LIKE MAKT-MAKTX
*" VALUE(CREATE_FIELD_NAME) LIKE MAKT-MAKTX
*" VALUE(UPDATE_FIELD_NAME) LIKE MAKT-MAKTX
*" EXPORTING
*" VALUE(MAX_DATE) LIKE SY-DATUM
DATA: CREATE_DATE LIKE MCHA-ERSDA VALUE '19000101',
UPDATE_DATE LIKE MCHA-LAEDA VALUE '19000101'.
SELECT MAX(CREATE_FIELD_NAME) INTO (CREATE_DATE) FROM (TABLE_NAME).
ENDSELECT.
*SELECT MAX((UPDATE_FIELD_NAME)) INTO (UPDATE_DATE) FROM (TABLE_NAME).
*ENDSELECT.
IF CREATE_DATE > UPDATE_DATE.
MAX_DATE = CREATE_DATE.
ELSE.
MAX_DATE = UPDATE_DATE.
ENDIF.
IF MAX_DATE = '19000101'.
MAX_DATE = SY-DATUM.
ENDIF.
ENDFUNCTION.Max is right, you need the spaces, as in my example.
You might like to try this:
data: l_CREATE_FIELD_NAME) LIKE MAKT-MAKTX,
l_UPDATE_FIELD_NAME) LIKE MAKT-MAKTX.
DATA: CREATE_DATE LIKE MCHA-ERSDA VALUE '19000101',
UPDATE_DATE LIKE MCHA-LAEDA VALUE '19000101'.
concatenate 'MAX(' create_name ')' into l_create_name separated by space.
concatenate 'MAX(' update_name ')' into l_update_name separated by space.
SELECT SINGLE (l_CREATE_FIELD_NAME)
INTO CREATE_DATE FROM (table_name).
SELECT SINGLE (l_updATE_FIELD_NAME)
INTO updATE_DATE FROM (table_name).
IF CREATE_DATE > UPDATE_DATE.
MAX_DATE = CREATE_DATE.
ELSE.
MAX_DATE = UPDATE_DATE.
ENDIF.
IF MAX_DATE = '19000101'.
MAX_DATE = SY-DATUM.
ENDIF.
If it still fails please post the latest abap code for us to check.
Maybe you are looking for
-
when I am trying to download IPad updates I get the message "Your Apple ID has been disabled". What does this mean? How do I get my ID enabled again? Still got more than 50$ in my account !
-
How do I get from a menu at login to CLI on a SG300-28 Switch?
I have a SG300-28 and would like to configure through CLI not menu. How do I get to Cli from Menu? I am running Boot version 1.0.0.4 and SW version 1.0.0.27, Do I need to upgrade both boot and SW or Just SW?
-
How do i remove email addresses that have been imported from my email account?
Im not sure how i did it but somehow i have a ton of email addresses in my contacts and I want to remove them....Is there a way to do this without have to remove them one at a time.....I hardly send anyone emails from my contact list and would love t
-
Graphic labels in Visual Composer
Hİ all, Is there any possibility to show the labels on data series for the column or line graphics in Visual Composer? Thanks..
-
Hi, I am facing problem in MBEW table while releasing the billing document to accounting this is in 3rd party scenario. Even when i am doing cedit memo at that time also it shows same error. Rudra