Execute Immediate for an IF statement
All,I'm trying to run an IF statement dynamically at execution using the Execute Immediate statement, but I am gettign error... The error is PLS-00201: identifier 'V_BARG_UNIT' must be declared
The condition and the result for the case statement is stored in a test_dynamic_if_tbl table that has 2 columns.
condition = v_barg_unit = 'ABC' and result_val = v_bg_group: = "93"
Yet the code...
DECLARE
cmd VARCHAR2 (4000);
v_barg_unit VARCHAR2 (10): = 'ABC ';
v_bg_group VARCHAR2 (10): = ' ';
ABC of the CURSOR
IS
SELECT * FROM test_dynamic_if_tbl;
BEGIN
FOR c1 IN abc
LOOP
cmd: =.
"Start."
If ' | C1.condition
|| "THEN."
|| C1.result_val
|| ' ; End if;
End;';
Dbms_output.put_line ('cmd: ' | cmd);
Cmd EXECUTE IMMEDIATE.
Dbms_output.put_line (' status: ' | c1.condition);
Dbms_output.put_line ('result_value: ' | c1.result_val);
Dbms_output.put_line ('v_bg_group: ' | v_bg_group);
END LOOP;
END;
What I am doing wrong?
Thanks for your help in advance,
Vinay
Published by: user652279 on September 16, 2009 11:45
Published by: user652279 on September 16, 2009 11:47
Well, dynamic sql is executed in a separate context and scope. V_barg_unit stated in your code is therefore not visible in the dynamic PL/SQL block you run. You can pass block dynamic PL/SQL using the variable binding and the USING clause:
DECLARE
cmd VARCHAR2 (4000);
v_barg_unit VARCHAR2 (10) := 'ABC';
v_bg_group VARCHAR2 (10) := ' ';
CURSOR abc
IS
SELECT * FROM test_dynamic_if_tbl;
BEGIN
FOR c1 IN abc
LOOP
cmd :=
'declare v_barg_unit VARCHAR2 (10);
Begin
v_barg_unit := :v_barg_unit;
If ' || c1.condition
|| ' THEN '
|| c1.result_val
|| ' ; End if;
End;';
DBMS_OUTPUT.put_line ('cmd: ' || cmd);
EXECUTE IMMEDIATE cmd USING v_barg_unit,OUT v_bg_group;
DBMS_OUTPUT.put_line ('Condition: ' || c1.condition);
DBMS_OUTPUT.put_line ('result_value: ' || c1.result_val);
DBMS_OUTPUT.put_line ('v_bg_group: ' || v_bg_group);
END LOOP;
END;
However, since you do not know what name will be stored in test_dynamic_if_tbl my suggestion is not a generic solution.
SY.
Published by: Solomon Yakobson September 16, 2009 12:07
Tags: Database
Similar Questions
-
Hi all
I'm using Oracle 11 g Release2.
I need to use the forall with EXECUTE IMMEDIATE statement and the content of string inside EXECUTE IMMEDIATE is a SELECT instead of the usual DML statement.
Objective: In this SELECT statement, I'll be passing of data using the "USING" clause in FORALL statement
When I try to use it, am getting error found "no DATA".
Please advice if I use FORALL and EXECUTE IMMEDIATE on a SELECT statement?
I can share the scenario...
Thank you!>
Please advice if I use FORALL and EXECUTE IMMEDIATE on a SELECT statement?
>
No - you can't. See the specifications of the PL/SQL language
http://docs.Oracle.com/CD/E14072_01/AppDev.112/e10472/forall_statement.htm
>
dml_statementAn INSERT, UPDATE, or DELETE static or dynamic statement making reference at least a collection in its VALUES or the WHERE clause. Performance benefits apply only to references to the collection that use as an index index_name.
-
The concatenation of the EXECUTE IMMEDIATE statement.
Hi all
The situation is that I am trying to find all customers whose Date of birth (DOB) is greater than or equal to 150 days. Unfortunately, when I run this SQL I get the error message:
(1) ORA-00904: "FEB": invalid identifier
(1) ORA-06512: at line 4 level
I'm not sure what I should do to solve this problem, I hope you can help.
Note: I have to encapsulate this statement in an immediate execution due to problems of roles/Privilidge. In the full version, I also use AUTHID CURRENT_USER.
Edited by: DaveyB February 23, 2012 08:39DECLARE curr_date Date := SYSDATE; BEGIN EXECUTE IMMEDIATE ' SELECT cust_name, cust_dob, -- date of birth cust_shoe_size FROM tblCustDetails c WHERE SUBSTR(c.cust_name, 1,7) = ''Bob'' AND cust_dob >= NEXT_DAY(' || curr_date ||' - 150 , ''SATURDAY'') '; END;
The right way to use dynamic queries like this would get stuck in the variables you use... for example
DECLARE curr_date Date := SYSDATE; BEGIN EXECUTE IMMEDIATE ' SELECT cust_name, cust_dob, -- date of birth cust_shoe_size FROM tblCustDetails c WHERE SUBSTR(c.cust_name, 1,7) = ''Bob'' AND cust_dob >= NEXT_DAY(:1 - 150 , ''SATURDAY'') ' USING curr_date; END;
However, you also have to take into account the fact that the results of the query are currently disappearing into the ether, so you need an INTO statement...
DECLARE curr_date Date := SYSDATE; BEGIN EXECUTE IMMEDIATE ' SELECT cust_name, cust_dob, -- date of birth cust_shoe_size FROM tblCustDetails c WHERE SUBSTR(c.cust_name, 1,7) = ''Bob'' AND cust_dob >= NEXT_DAY(:1 - 150 , ''SATURDAY'') ' INTO var1, var2, var3 USING curr_date; END;
ensure that your variables are declared as appropriate, and then...
... you have to take into account the possibility that there is more than 1 line returned, in which case you will have the results of the query to a collection.
Of course, it is useless for dynamic SQL statements in the first place...
DECLARE curr_date Date := SYSDATE; BEGIN SELECT cust_name, cust_dob, -- date of birth cust_shoe_size INTO var1, var2, var3 FROM tblCustDetails c WHERE SUBSTR(c.cust_name, 1,7) = 'Bob' AND cust_dob >= NEXT_DAY(curr_date - 150 , 'SATURDAY'); END;
still, you should always take into account the bulk collection in a collection if it were going to be more than one row returned.
-
dml execution 2 statements at a time with the help of execute immediate
I would like to run 2 DML statements at a time with the help of execute immediate usefulness,.
ex:
update employee set ename = 'Chantal' where eno = 123;
Update dept set dname = 'IT' where dno = 345;
I want the two statement to execute at a time, but I don't want to use any loop as well.
Thanks in advanceYou can use:
execute immediate 'begin update employee set ename='jagadeesh' where eno=123; update dept set dname='IT' where dno=345; end;';
Although updates will always run one after the other.
If you want the simultaneous treatment, then you need to use DBMS_JOB and DBMS_SCHEDULER to plan both work to run to run each of the statements independently.
-
suffixing date in the EXECUTE IMMEDIATE statement
Hi could one please let me know how I can the suffix of the name of the table to the year, which the calculation is being in the select statement. Output should be as an example: test_2007declare abc number; begin select (to_char(sysdate, 'YYYY') - 4) into abc from dual; EXECUTE IMMEDIATE 'CREATE TABLE test_&abc as select * from emp where 1=2'; end;
You can do it like this
BEGIN EXECUTE IMMEDIATE 'CREATE TABLE test_' || (TO_CHAR ( SYSDATE, 'YYYY') - 4) || ' as select * from emp where 1=2'; END;
You can use your variable as well, if this can change dynamically.
DECLARE abc NUMBER; BEGIN SELECT (TO_CHAR ( SYSDATE, 'YYYY') - 4) INTO abc FROM DUAL; EXECUTE IMMEDIATE 'CREATE TABLE test_'||abc||' as select * from emp where 1=2'; END;
Edited by: g. March 1, 2011 11:49
-
Pass Pl/sql table in the USING clause in the EXECUTE IMMEDIATE statement
Getting error when I try to pass the PL/SQL table in the USING clause in the EXECUTE IMMEDIATE statement:
Declare
result NUMBER;
TYPE values_tab IS TABLE OF NUMBER INDEX OF directory;
lv_tab values_tab;
lv_exp varchar2 (300);
lv_exec varchar2 (300);
BEGIN
lv_tab (1): = 5;
lv_tab (2): = 48;
lv_tab (3): = 7;
lv_tab (4): = 6;
lv_exp: = ': + b1: b2 + (: b3 *: b4)';
lv_exec: = 'SELECT'. lv_exp | ' THE DOUBLE '.
IMMEDIATE EXECUTION
lv_exec
IN
result
USING
lv_tab;
DBMS_OUTPUT. Put_line (result);
END;
/
Error on line 1
ORA-06550: line 20, column 12:
PLS-00457: expressions must be SQL types
ORA-06550: line 15, column 8:
PL/SQL: Statement ignored
I am trying to evaluate the expression ': + b1: b2 + (: b3 *: b4) "which is stored in the table. This table has different expressions (expressions about 300). I want to use the bind variable in the expression because each expression evaluated thousands of time may be more in some cases. If I use bind variable can he fill pool.
Is there a way I can pass parameters with the HELP of (IN) dynamically instead of write "help lv_tab (1), lv_tab (2), lv_tab (3), lv_tab (4)? As number of change of the input parameters depend on the expression in the table.
If it is possible please suggest any other ideas/approaches
Help, please...
Published by: satnam on June 11, 2009 11:50Well, you keep changing faster reqs that I can follow. In any case, assuming that N-th variable bind (left to right) corresponds to n-th collection item:
Declare result NUMBER; lv_tab values_tab := values_tab(); lv_exp varchar2(300); lv_exec varchar2(300); lv_i number := 0; BEGIN lv_tab.extend(4); lv_tab(1) := 5; lv_tab(2) := 48; lv_tab(3) := 7; lv_tab(4) := 6; lv_exp := ':5000135+:5403456+(:5900111*:5200456)'; lv_exec := lv_exp; While regexp_like(lv_exec,':\d+') loop lv_i := lv_i + 1; lv_exec := REGEXP_REPLACE(lv_exec,':\d+',':b(' || lv_i || ')',1,1); end loop; lv_exec := 'BEGIN :a := ' || lv_exec || '; END;'; DBMS_OUTPUT.PUT_LINE(lv_exec); EXECUTE IMMEDIATE lv_exec USING OUT result,IN lv_tab; DBMS_OUTPUT.PUT_LINE(result); END; / BEGIN :a := :b(1)+:b(2)+(:b(3)*:b(4)); END; 95 PL/SQL procedure successfully completed. SQL>
SY.
-
How to run an EXECUTE IMMEDIATE statement in an interactive report
Hi all!!
I need to make a dynamic construction of a query to run in an interactive report, but the Source of the region only allows SELECT simple instructions. There is no way to run an EXECUTE IMMEDIATE statement in a Source of interactive report region?
Cordially Pedro.Hi Pedro,
Reduce the size of the varchar2 32000 1000 (or, up to a maximum of 4000)
Andy
-
Error in EXECUTING IMMEDIATE statement
Hello again, friends, I have this block:
It shows me:SET SERVEROUTPUT ON DECLARE columnCount pls_integer; counter pls_integer := 1; columnName VARCHAR2(50); sqlQuery VARCHAR2(1000); columnLength pls_integer; dataType VARCHAR2(50); rowSize pls_integer; BEGIN rowSize := 0; SELECT COUNT(TABLE_NAME) INTO columnCount FROM USER_TAB_COLUMNS WHERE TABLE_NAME = 'PERSON'; WHILE counter <= columnCount LOOP SELECT COLUMN_NAME, DATA_TYPE INTO columnName, dataType FROM USER_TAB_COLUMNS WHERE TABLE_NAME = 'PERSON' AND COLUMN_ID = counter; IF dataType = 'BLOB' THEN sqlQuery := q'[SELECT NVL(DBMS_LOB.GETLENGTH(]' || columnName || q'[), 0) FROM PERSON WHERE UUID = '999E5241-02AD-45fc-958A-CE808B0F56A6']'; ELSIF dataType = 'SDO_GEOMETRY' THEN sqlQuery := q'[SELECT 500 FROM DUAL]'; ELSE sqlQuery := q'[SELECT NVL(DBMS_LOB.GETLENGTH(CAST(]' || columnName || q'[ AS VARCHAR2(4000))), 0) FROM PERSON WHERE UUID = '999E5241-02AD-45fc-958A-CE808B0F56A6';]'; END IF; EXECUTE IMMEDIATE sqlQuery INTO columnLength; -- Error here rowSize := rowSize + columnLength; counter := counter + 1; END LOOP; DBMS_OUTPUT.PUT_LINE(rowSize); END;
DECLARE
*
ERROR on line 1:
ORA-00911: invalid character
ORA-06512: at line 21
What could be?Get rid of semi-colons in:
sqlQuery: = q'[SELECT NVL (DBMS_LOB. [(GETLENGTH (CAST (]' | columnName | q'[AS VARCHAR2 (4000)])), 0) OF THE PERSON WHERE the UUID = '999E5241-02AD-45-CF - 958 A-CE808B0F56A6';]';
Must be:
sqlQuery: = q'[SELECT NVL (DBMS_LOB. [(GETLENGTH (CAST (]' | columnName | q'[AS VARCHAR2 (4000)])), 0) OF THE PERSON WHERE the UUID = "999E5241-02AD-45-CF - 958 A-CE808B0F56A6"] ";
SY.
-
use of execute immediate with variables
Hello
I want to update a table with immediate execution, but I need to use the variable to run immediately. I typed a code, but the code below does not work and I m getting the error ORA-00936. Something wrong with this code?
WHILE v_acu_payment_amount > 0
LOOP
BEGIN
SELECT emplid, amount_past_due
IN v_emplid, v_vade_tutari
OF vade_temp
WHERE the sira = v_sirano AND emplid IN (r1.emplid);
END;
v_acu_payment_amount: = v_acu_payment_amount - v_vade_tutari;
Dbms_output.put_line ('paymentamount2' | v_acu_payment_amount);
IF v_acu_payment_amount > 0
THEN
EXECUTE IMMEDIATE "update vade_temp set AMOUNT_PAST_DUE = to_number('||) 0
|| ') where sira = to_number (')
|| v_sirano
|| ')';
ON THE OTHER
Temp: = v_acu_payment_amount * (-1);
Dbms_output.put_line (temp);
EXECUTE IMMEDIATE "update vade_temp set AMOUNT_PAST_DUE = to_number (')"
|| Temp
|| ') where sira = to_number (')
|| v_sirano
|| ')';
END IF;
COMMIT;
v_sirano: = v_sirano + 1;
END LOOP;
Kind regards
Gunce
In addition, you have no dynamic SQL for this
[code]
WHILE v_acu_payment_amount > 0
LOOP
BEGIN
SELECT emplid, amount_past_due
IN v_emplid, v_vade_tutari
OF vade_temp
WHERE the sira = v_sirano AND emplid IN (r1.emplid);
END;
v_acu_payment_amount: = v_acu_payment_amount - v_vade_tutari;
Dbms_output.put_line ('paymentamount2' | v_acu_payment_amount);
IF v_acu_payment_amount > 0
THEN
UPDATE vade_temp
SET amount_past_due = 0
WHERE the sira = v_sirano;
ON THE OTHER
Temp: = v_acu_payment_amount * (-1);
Dbms_output.put_line (temp);
UPDATE vade_temp
SET AMOUNT_PAST_DUE = temp
where sira = v_sirano;
END IF;
COMMIT;
v_sirano: = v_sirano + 1;
END LOOP;
[/ code]
But if you need dynamic sql statements, then you should use bind variables as
...
EXECUTE IMMEDIATE "UPDATE vade_temp SET amount_past_due =: b1 WHERE sira =: b2' using 0, v_sirano;"
...
HTH
-
Execute Immediate with DOF and TABLE() - error ORA-22905
Hello
I have a problem trying to use a user defined the Type of the Table in a statement Execute Immediate containing a CREATE TABLE statement.
Is there no work around for this problem?
The actual code for the SELECT * OF TABLE(:T) is dynamic and slow. That's why I try to avoid to create/fill the table in two steps (as does with MY_TABLE1). Also, in this case, I can't use SELECT * but must specify all the columns (amount variable and over 100 columns).
CREATE TYPE MY_TABLE_TYPE AS TABLE OF VARCHAR2(30); / DECLARE MT MY_TABLE_TYPE; BEGIN SELECT * BULK COLLECT INTO MT FROM DUAL; -- Two steps EXECUTE IMMEDIATE 'CREATE TABLE MY_TABLE1 (A VARCHAR2(30))'; EXECUTE IMMEDIATE 'INSERT INTO MY_TABLE1 SELECT * FROM TABLE(:T)' USING MT; -- OK -- One step EXECUTE IMMEDIATE 'CREATE TABLE MY_TABLE2 AS SELECT * FROM TABLE(:T)' USING MT; -- ERROR ORA-22905 END; /
byee
Andrea
In my view, the error message is incorrect or the less misleading. Bind variables cannot be used in DDL:
SQL > declare
2 number of v_var: = 99;
3. start
4 run immediately "' create the my_table2 in select table: double T" using v_var; "
5 end;
6.
declare
*
ERROR on line 1:
ORA-01027: bind variable not allowed for data definition operations
ORA-06512: at line 4 levelSQL >
What you could do is use the package variable:
SQL > CREATE OR REPLACE
2 PACKAGE PKG1
3 EAST
4 MT MY_TABLE_TYPE;
5 FUNCTION GET_MT
6 RETURN MY_TABLE_TYPE;
7 END;
8.Package created.
SQL > CREATE OR REPLACE
PACKAGE 2 BODY PKG1
3 EAST
4 GET_MT FUNCTION
5 RETURN MY_TABLE_TYPE
6 EAST
7. START
8 RETURN MT;
9 END;
10 END;
11.Package body created.
SQL > DROP TABLE MY_TABLE1 PURGE
2.Deleted table.
SQL > DROP TABLE MY_TABLE2 PURGE
2.
DROP TABLE MY_TABLE2 PURGE
*
ERROR on line 1:
ORA-00942: table or view does not existSQL > START
2. SELECT * BULK COLLECT INTO PKG1.MT FROM DUAL;
3 - two steps
4 RUN IMMEDIATELY "CREATE TABLE MY_TABLE1 (A VARCHAR2 (30))';"
5 IMMEDIATELY EXECUTE "INSERT INTO MY_TABLE1 SELECT * FROM TABLE (PKG1." GET_MT)';
6 - one step
7 IMMEDIATE EXECUTION "CREATE TABLE MY_TABLE2 AS SELECT * FROM TABLE (PKG1." GET_MT)';
8 END;
9.PL/SQL procedure successfully completed.
SQL > select * from my_table1;
A
------------------------------
XSQL > select * from my_table2;
COLUMN_VALUE
------------------------------
XSQL >
SY.
-
How to run execute immediate with variables
Hi friends,
How to run execute immediate with variables in v_stmt below?
I don't know how to declare value I have here.
Regds,Set serveroutput on; DECLARE i VARCHAR (20); v_stmt VARCHAR2 (100); CURSOR c IS SELECT sqlid FROM temp1; BEGIN OPEN c; LOOP FETCH c INTO i; EXIT WHEN c%NOTFOUND; DBMS_OUTPUT.put_line (i); v_stmt := 'select * from table(dbms_xplan.display_cursor('&i',null))' execute immediate v_stmt; END LOOP; CLOSE c; END; /
Kunwar.You must first use a variable binding (named ': v' in the SQL statement in my example):
set serveroutput on; DECLARE i VARCHAR (20); v_stmt VARCHAR2 (100); CURSOR c IS -- modified for a quick test SELECT sql_id FROM v$sql where child_number > 2; BEGIN OPEN c; LOOP FETCH c INTO i; EXIT WHEN c%NOTFOUND; DBMS_OUTPUT.put_line (i); v_stmt := 'select * from table(dbms_xplan.display_cursor(:v,null))'; execute immediate v_stmt using i; END LOOP; CLOSE c; END; /
However because your SELECT statement returns multiple lines, you need to adapt your code to process all rows returned (as already suggested in first response to your message).
Instead of using the PL/SQL, I recommend you to generate a SQL file using only SQL, and then run the generated SQL file.
For example:spool edx.sql set serveroutput on declare v_stmt varchar2(100); v_q char(1):=''''; begin dbms_output.put_line('spool edx.log'); for s in (select sql_id from v$sql where child_number >2) loop dbms_output.put_line('select * from table(dbms_xplan.display_cursor(' || v_q || s.sql_id || v_q || ',null));'); end loop; dbms_output.put_line('exit'); end; / spool of
This generates a file similar to:
spool edx.log select * from table(dbms_xplan.display_cursor('5rygsj4dbw6jt',null)); select * from table(dbms_xplan.display_cursor('5rygsj4dbw6jt',null)); select * from table(dbms_xplan.display_cursor('5rygsj4dbw6jt',null)); select * from table(dbms_xplan.display_cursor('fsbqktj5vw6n9',null)); select * from table(dbms_xplan.display_cursor('6q42j0018w7t8',null)); select * from table(dbms_xplan.display_cursor('a5mmhrrnpwjsc',null)); select * from table(dbms_xplan.display_cursor('3c1kubcdjnppq',null)); select * from table(dbms_xplan.display_cursor('3c1kubcdjnppq',null)); select * from table(dbms_xplan.display_cursor('9gkq7rruycsjp',null)); select * from table(dbms_xplan.display_cursor('f0wj261bm8snd',null)); select * from table(dbms_xplan.display_cursor('ab3swhv5g138y',null)); select * from table(dbms_xplan.display_cursor('6vgvyh4xw9c5g',null)); select * from table(dbms_xplan.display_cursor('ak5crjygnpk60',null)); select * from table(dbms_xplan.display_cursor('9p6bq1v54k13j',null)); select * from table(dbms_xplan.display_cursor('19x1189chq3xd',null)); select * from table(dbms_xplan.display_cursor('7sx5p1ug5ag12',null)); select * from table(dbms_xplan.display_cursor('730vdzhng6m6g',null)); select * from table(dbms_xplan.display_cursor('730vdzhng6m6g',null)); select * from table(dbms_xplan.display_cursor('0v3dvmc22qnam',null)); select * from table(dbms_xplan.display_cursor('0v3dvmc22qnam',null)); select * from table(dbms_xplan.display_cursor('a1zv6wju3ftgv',null)); select * from table(dbms_xplan.display_cursor('7ng34ruy5awxq',null)); select * from table(dbms_xplan.display_cursor('7ng34ruy5awxq',null)); select * from table(dbms_xplan.display_cursor('b2gnxm5z6r51n',null)); select * from table(dbms_xplan.display_cursor('b2gnxm5z6r51n',null)); select * from table(dbms_xplan.display_cursor('g4gp07gt2z920',null)); select * from table(dbms_xplan.display_cursor('1gu8t96d0bdmu',null)); select * from table(dbms_xplan.display_cursor('g00cj285jmgsw',null)); select * from table(dbms_xplan.display_cursor('g00cj285jmgsw',null)); select * from table(dbms_xplan.display_cursor('g00cj285jmgsw',null)); select * from table(dbms_xplan.display_cursor('bn4b3vjw2mj3u',null)); select * from table(dbms_xplan.display_cursor('38243c4tqrkxm',null)); select * from table(dbms_xplan.display_cursor('2abjfnvy5rkyg',null)); select * from table(dbms_xplan.display_cursor('350f5yrnnmshs',null)); select * from table(dbms_xplan.display_cursor('350f5yrnnmshs',null)); select * from table(dbms_xplan.display_cursor('3s1yukp05bzg6',null)); select * from table(dbms_xplan.display_cursor('3s1yukp05bzg6',null)); select * from table(dbms_xplan.display_cursor('1tgukkrqj3zhw',null)); exit PL/SQL procedure successfully completed.
Edited by: P. Forstmann March 20, 2013 19:06
Edited by: P. Forstmann March 20, 2013 19:33
-
How to set multiple parameters in a sql EXECUTE IMMEDIATE
I want to set nls_language and nls_date_language and set them as different languages. fnd_global.set_nls_context () does not work. So I think I can use the EXECUTE IMMEDIATE and add them together into a single statement. But I don't know how to make that happen. Can someone help me? Thank you very much.
PS: It must be done in a single call.928091 wrote:
Hey, thanks for your reply, I forgot to mention that it should be in the begin-end block, do you know how?EXEC is a more SQL API which can be used to execute the PL/SQL code. What it does is put the PL/SQL code in a BEGIN block... END of block and run.
So, you can also manually put code in a BEGIN block... END of block and run like that.
SQL> begin 2 execute immediate q'[begin execute immediate 'alter session set nls_date_language = ''AMERICAN'''; execute immediate 'alter session set nls_language = ''AMERICAN'''; end;]'; 3 end; 4 / PL/SQL procedure successfully completed. SQL>
And I don't understand what possible benefit he made a single run in a BEGIN block... Terminate during execution of multiples.
Why this code is not in your interest, something like this
begin execute immediate 'alter session set nls_date_language = ''AMERICAN'''; execute immediate 'alter session set nls_language = ''AMERICAN'''; end; /
-
Use of EXECUTE IMMEDIATE with XML
Database version: 10.2.0.3.0 - 64bi
Hi all
I have an xml that is stored in a table, xmltype column.
I target insert tables including column names and xml nodes are the same.
using the table of all_tab_columns I will generate columns must be passed to xmltable.
all these data, I'll store them in variables and finally proceed to xmltable as below
I just want to know using execute immediate is good to use XML?
SQL_STMT: = 'insert'. table_name | ' ( '|| V_COLUMN_NAME |') ' ;
SQL_STMT: = SQL_STMT | ' SELECT '. V_XTAB_COLUMN_NAME |
"FOR TO_XML,
XMLTABLE(' || v_xpath ||)
"PASSAGE XML_VALUE
columns | V_COLUMNS_DATA_TYPE
||') XTAB
WHERE Seq_NO = ' | P_SEQUENCE_NO;
RUN IMMEDIATELY SQL_STMT;
Thank you and best regards,
RubyHi Ruby,
I just want to know using execute immediate is good to use XML?
Sorry, but this question does not make much sense.
Whether or not dynamic SQL means XML is not relevant, the statement will end up as a cursor in the shared pool independently and be executed or retrieved as any other.If I were you, I'd be worried on the statement do not use bind variables, or the need for dynamic SQL in the first place.
Why should you use dynamic SQL?
If it's laziness, because there are a lot of columns to write down, then it is a bad reason. -
EXECUTE IMMEDIATE throws exception NO_DATA_FOUND when it should not
Hi all
I wrote an anonymous PL/SQL block that, on paper, should be OK, but one of its commands EXECUTE IMMEDIATE is runnning slowing prematurely and throw an exception NO_DATA_FOUND, when it should not because I know that there is data in the table.
Here is the line that I suspect is out of steam after 8 attempts:
---
immediately execute "INSERT INTO ple101.circular35 (ID, SLAVETABLEID, SLAVEITEMID, MASTERTABLEID, MASTERITEMID, PRO) (SELECT * FROM SLAVETASKS where mastertableid =' |)" subTableID | "and masteritemid =' |" subitemID |' and subtableid =' | mastTableID | "and SLAVEITEMID =' |" mastItemId |') ' ;
------
It is supposed to search the entire SLAVETASKS table for documents that match the WHERE clause, but gives up after 8 cycles.
Table SLAVETASKS got 10228 records.
The above statement (and the whole of the PLSQL block) is good in that it gets records if they are wthin the first 8 records in the SLAVETASKS table, but like I said after 8 records, to throw an exception NO_DATA_FOUND.
Published by: user10390960 on April 28, 2012 03:21
Published by: user10390960 on April 28, 2012 03:22
Published by: user10390960 on April 28, 2012 03:23
Published by: user10390960 on April 28, 2012 05:08Hi Goofy,
The above script is for PLSQL and if you want to display the data in circular 35 follow like that
# Save the code below in the sqlscript for example. circular_process_display. SQL and run as
sqlplus
/ @ @circular_process_display.sql -- CREATE TABLE circular 35 CREATE TABLE circular35 .... .. / -- Execute PLSQL Block as given in my earlier post DECLARE ... BEGIN ... EXCEPTION ... END; / -- Run SQL to display output on screen. SET SERVEROUTPUT ON SIZE 100000 FEEDBACK ON LINESIZE 200; SELECT * FROM circular35 / {code}
-
Execute Immediate is not working well
We need to find tables with the same name and columns but these columns whose data type is different. The SQL below detects the difference.
Based on the SQL above, I created the procedure mentioned below which takes a base as input parameter schema and find these tables with the same name but have different data type columns. It generates dynamic SQL code properly (you can see in DBMS_OUTPUT) but for some reason, the INSERT is not the case with EXECUTE IMMEDIATE. No idea why?select a.table_name, a.column_name, a.data_type ||'---> '||a.owner BASE_SCHEMA_TYPE, b.data_type ||'---> '||B.owner COMPARED_SCHEMA_TYPE from all_tab_columns a, all_tab_columns b where a.owner = 'SCOTT' and b.owner = 'HR' and a.table_name=b.table_name and a.column_name=b.column_name and a.data_type!=b.data_type
Structure of the table being INSERT edcreate or replace procedure admin.compare_data_types (p_baseschema in varchar2) is v_sql varchar2(2000); begin delete from admin.differences; commit; for rec in (select username from dba_users where Username IN ( 'HR')) loop v_sql := 'insert into admin.differences select a.table_name, a.column_name, a.data_type ||''---> ''||a.owner BASE_SCHEMA_TYPE, b.data_type ||''---> ''||B.owner COMPARED_SCHEMA_TYPE from all_tab_columns a, all_tab_columns b where a.owner = '''||p_baseschema||''' and b.owner = '''||rec.username||''' and a.table_name=b.table_name and a.column_name=b.column_name and a.data_type!=b.data_type'; dbms_output.put_line (v_sql); execute immediate v_sql; --commit; -- Doesn't make any difference --v_sql := null; -- Doesn't make any difference end loop; commit; end compare_data_types;
create table admin.DIFFERENCES ( TABLE_NAME VARCHAR2(200), COLUMN_NAME VARCHAR2(200), BASE_SCHEMA_TYPE VARCHAR2(100), COMPARED_SCHEMA_TYPE VARCHAR2(100) )
How about putting in a debug message to see what he does...
(don't forget to ' set serveroutput we "before...)
create or replace procedure admin.compare_data_types (p_baseschema in varchar2) is v_sql varchar2(2000); begin delete from admin.differences; commit; for rec in (select username from dba_users where Username IN ( 'HR')) loop insert into admin.differences select a.table_name ,a.column_name ,a.data_type ||'---> '||a.owner BASE_SCHEMA_TYPE ,b.data_type ||'---> '||B.owner COMPARED_SCHEMA_TYPE from all_tab_columns a ,all_tab_columns b where a.owner = p_baseschema -- Procedure Paramater and b.owner = rec.username and a.table_name = b.table_name and a.column_name = b.column_name and a.data_type!=b.data_type; dbms_output.put_line('Rows Inserted for '||rec.username||': '||sql%rowcount); end loop; commit; end compare_data_types;
And see if it is one) enter the loop and b) the number of rows is inserted during the process.
If it displays 0 rows inserted, then there's something of logically wrong with your select statement, as is return nothing.
Maybe you are looking for
-
Satellite U845W / U840W / U800W - screen is coded using Linux
Dear Sir or Madam I install linux in addition to Windows on these laptops, but the laptop screen is blurred, while the external display which I attach HDMI is fine. Please tell us the display settings. Thank you Bo
-
Hello looking for HP scanjet C7716A,. That seems difficult to find on the net can you help me to find it, I look everywhere
-
Can not install HP 4000 with JetDirect on Windows 8.1 card
Hi guys (and girls) I hope someone can help. Tried to install a HP4000 with a JetDirect card on my network for hours now. My Windows 8.1 x 64 just does not see the printer on the network. (I had a Dell on the network before without problem. When I pr
-
Bitmap.getBitmapResource bug?
Hello I try to use Bitmap.getBitmapResource and it cannot read the file. I tested this code DataInputStream din = new DataInputStream(this.getClass().getResourceAsStream(fileName)); byte[] buff1 = new byte[din.available()]; din.readFully(buff1); Bitm
-
Audition - First timeline: version the most recent merger with a precedent?
So, I'm working on this company productiion and I handed the film to my client. All finished and well. But as in the production of each company it is bound to come an Afterburner frome some guy or GAL of marketing or corporate communications. It happ