Dynamic Query(execute immediate)
Greetings,
VERSION:-10 g
Name of the table column is the name of the month which I store in a condition variable that I have to pass the names of columns at runtime of the value
TABLE DEALER_MAST
NUMBER OF DEALER_DIM_ID
NUMBER OF APR
MAY ISSUE
JUNE ISSUE
NUMBER OF JUL
AUGUST ISSUE
NUMBER OF MS
I now have the code example below in my procedure
v_dealer VARCHAR2 (3);
XYZ varchar2 (2000);
SELECT TO_CHAR (SYSDATE, 'MY') IN THE DOUBLE V_DEALER;
DECLARE CURSOR a1 IS SELECT DEALER_ID FROM DEALER_MAST;
BEGIN
FOR j IN a1
loop
COUNT (*) SELECT IN DEALER_COMM FROM subs_fact
WHERE TO_CHAR (ACTIVATION_DATE, 'Mon - yy') = (select to_char (add_MONTHS(sysdate,-2), 'mon-yy') FROM dual)
- AND TAB_ELEG = 1
and DEALER_ID = j.DEALER_ID;
-Dynamicaly passing the name of column
XYZ: = 'SELECT'. V_DEALER | "IN DEALER_MAST FROM DEALER_MAST WHERE DEALER_DIM_ID =' | j.DEALER_DIM_ID;
run immediately (XYZ);
/*
AFTER immediate execution of the query should be as
SEVEN ELECTED IN DEALER_MAST FROM DEALER_MAST WHERE DEALER_DIM_ID = 24345
But not to store the data in the variable & gives error like key word missing on run immediately (XYZ);
*/
If (DEALER_MAST > 2) can
---
--
end if;
ERROR:-do not store data in the variable & gives error like key word missing on run immediately (XYZ);
Thanks in advance
Maybe
l_var: = j.DEALER_DIM_ID;
XYZ: = "SELECT" | TO_CHAR (sysdate, 'MY'). ' FROM DEALER_MAST WHERE DEALER_DIM_ID =: l_var';
EXECUTE IMMEDIATE XYZ
INTO DEALER_MAST
USING l_var;
Concerning
Etbin
Tags: Database
Similar Questions
-
Collection of the dynamic query
Hi All-
I'm trying to get the value of the collection through the dynamic query but I am facing some problem please let me know that I hurt.
Created a function like below to run the dynamic query to select statement
create or replace FUNCTION rfunGetColumnValue ( ColumnName VARCHAR2, TableName VARCHAR2, DefaultValue OUT VARCHAR2, Criteria VARCHAR2) RETURN VARCHAR2 IS ReturnValue VARCHAR2 (32767 byte) ; Stmt VARCHAR2 (32767 byte) ; BEGIN stmt := 'begin select '|| ColumnName || ' into :1 from table(:2) ' ||NVL ( Criteria, ' ') || ' Fetch First Row only ; EXCEPTION WHEN OTHERS THEN :2 := SQLERRM; end;' ; dbms_output.put_line (stmt) ; EXECUTE IMMEDIATE stmt USING OUT ReturnValue, OUT DefaultValue; --select Valueinto into Returnvalue from dual; RETURN ReturnValue; END;
Now, I created a folder in the Package
create or replace PACKAGE Collection_PKG is type Bank_rec is RECORD ( SNO NUMBER(10), BANKID NUMBER(5), BANKNAME VARCHAR2(50), BANKSC VARCHAR2(50), ADDEDIT varchar2(1), COMPID number(5), ISBULK number(1), ROWNO number(10), ERROR VARCHAR2(500) ); TYPE Bank_tbl IS TABLE OF Bank_rec; --type Bank_cur is ref cursor return Bank_rec; end Collection_PKG; end Collection_PKG; end Collec tion_PKG;
Now, when I'm Trying the code below
DECLARE V_EXECQUERYPARAM XMLTYPE:= XMLTYPE('<QueryParam> <BankXML> <Bank> <BankID>0</BankID> <BankSC><![CDATA[RCB]]></BankSC> <BankName><![CDATA[Royal challenger Bank]]></BankName> <IsBulk>0</IsBulk> <AddEdit>A</AddEdit> </Bank> </BankXML> </QueryParam>'); BEGIN Rspbanksave( v_SPParamList => V_EXECQUERYPARAM ); END;
create or replace PROCEDURE rspBankSave ( v_SPParamList XMLTYPE DEFAULT NULL) IS V_Addedit VARCHAR2 (1 CHAR) ; Bank_tbl Collection_PKG.BANK_TBL := Collection_PKG.BANK_TBL () ; BEGIN SELECT Row_number () OVER (ORDER BY 1), XT.BankID, XT.BankName, XT.BankSC, XT.AddEdit, v_CompID, XT.IsBulk, CAST (0 AS NUMBER (5)), CASE WHEN MBank.BankID IS NOT NULL THEN CASE WHEN XT.BankSC = MBank.BankSC AND XT.BankName = MBank.BankName THEN 'R104|Entry Already Exist,R114|Short Code Already Exist' WHEN XT.BankSC = MBank.BankSC THEN 'R114|Short Code Already Exist' WHEN XT.BankName = MBank.BankName THEN 'R104|Entry Already Exist' END ELSE NULL END Bulk collect INTO Bank_tbl FROM XMLTABLE ('//QueryParam/BankXML/Bank' PASSING v_SPParamList COLUMNS BankID NUMBER (5) PATH 'BankID', BankName VARCHAR2 ( 50) PATH 'BankName', BankSC VARCHAR2 ( 50) PATH 'BankSC', AddEdit VARCHAR2 ( 1) PATH 'AddEdit', IsBulk NUMBER (1) PATH 'IsBulk') XT LEFT JOIN MBank ON XT.BankID != MBank.BankID AND v_CompID = MBank.CompID AND (XT.BankSC = MBank.BankSC OR XT.BankName = MBank.BankName) ; v_TotalRowCount := SQL%ROWCOUNT; IF (v_TotalRowCount > 0) THEN BEGIN V_Addedit:=rfunGetColumnValue (ColumnName=> 'Upper(AddEdit)',TableName=>'table(Bank_tbl)',DefaultValue=>'',Criteria =>''); END; --SQL Code here END IF; END rspBankSave;
As I've suggested before:
To get the name of the table the column
and as others have:
Type global temporary Tables vs. table
If you give more context, more information about the bigger picture, more information about what you're trying to do and why you're going down the road you go down, you can return more useful information. But you seem reluctant to do.
For the moment, your recent posts seem to just raise the same type of question - why are you doing this?
The normal way to return data to a client is a refcursor.
It is the most effective way.
Not through collections that you seem to be put on the must-do approach.
-
Create a temporary table using EXECUTE IMMEDIATE
Hello
I need to create a report more complex which must have three different queries. I use a determined IF clause that the query that uses the report, according to the parameters.
I want to write the output to a csv file, so I created a PROCEDURE.
The first package of the procedure has the three SQL and the IF clause in order to determine the query that the report uses and stores the query code in a variable (v_sql), which is passed to the PROCEDURE that needs to write to the output of the report.
I tried to create a table with data from the request in order to make a LOOP and exit, something like this:
WRITE_FILE (errBUFF of the PROCEDURE OUT VARCHAR2, retCODE OUT VARCHAR2, v_sql IN VARCHAR2) IS
Contor NUMBER; BEGIN
EXECUTE IMMEDIATE v_sql; SLIDE G IS Select * from XXROR_REG_MF_TBL; -- -initialization -- retCode: = 0; errBUFF: = NULL; Contor: = 0; -- -cursor -- FOR X IN G LOOP APPS. FND_FILE. PUT_LINE) APPS. FND_FILE. OUTPUT, ....) I should mention that v_sql is something like:
CREATE GLOBAL TEMPORARY TABLE ACEs (SELECT * of the double);
I can't really do that, so I was wondering if you have any suggestions
Thank you
Claudiu
Hello
I don't think you need a table at all. You can set the cursor based on the SQL passed as parameter
https://docs.Oracle.com/database/121/LNPLS/dynamic.htm#LNPLS629
Concerning
Marcus
-
The use of bind variables in dynamic query created for Ref Cursor
Hello
I'm in a situation where there is a Ref cursor to which the query is built execution based on a loop. This is why the number of links would be known until the program runs.
The application is currently using literals instead of bind variables.
code snippet of the above is
strSql: = "select * from emp where 1 = 1 and ().
loop cursor1
If cond is true then
strSql = strSql | "ename = ' |" Cursor1.ColumnName;
end loop;
Open cursor2 for strSql;
How to use links in the example above.sb92075 wrote:
user13019948 wrote:
HelloHere is the code I have my trying to change literal-based link to the base.
What do you mean by "based bind?
who, what, how determines the values to be 'bound '?
He's referring to the coding style. He is currently using concatenated literal, and the goal is to change it to use the bindings.
If I understand this it is known as method 4 dynamic SQL and requires DBMS_SQL. There are examples autour but they vary according to the type of statement being generated - SELECT statements require column lists to be parsed, unlike the INSERT/UPDATE/DELETE.
This came up recently on my current project and I hit a demo. Here a table of names and values accepted procedure and had to build these in a single WHERE clause along the lines of
AND t_names(i) = t_values(i)
for an undetermined number of elements in the array. For this demonstration, I used a table that we called "attribute" (don't ask) which has columns including 'attribute_id' and 'name', and I need to build a query along the lines of
select description from attribute where attribute_id = :b1 and name = :b2
by the way '1012' and 'ISIN' respectively. (I use a table better and after a CREATE statement for her but I have to rush right now, sorry).
declare k_sql_base constant varchar2(500) := 'select description from attribute'; t_names constant varchar2_t := varchar2_t('attribute_id', 'name'); t_values constant varchar2_t := varchar2_t('1012', 'ISIN'); l_sql varchar2(500) := k_sql_base; l_rows_fetched integer := 0; l_value varchar2(4000); l_cursor_handle integer; begin -- Construct the SQL statement with column names and bind variables e.g. -- 'select description from mars.attribute where attribute_id = :b1 and name = :b2' for i in t_names.first .. t_names.last loop l_sql := l_sql || case i when t_names.first then ' where ' else ' and ' end || t_names(i) || ' = :b' || i; end loop; dbms_output.put_line('SQL statment = ' || l_sql); -- Parse the statement we built above (the remaining steps require a parsed cursor): l_cursor_handle := dbms_sql.open_cursor; dbms_sql.parse(l_cursor_handle, l_sql, dbms_sql.native); -- Associate the 1st column of output with variable l_value - required for SELECT statements: -- (actually the 3rd param here 'column' seems to be only used to get a datatype, in this case we want a string - -- dbms_sql.column_value actually extracts the value into a specified variable, which can be different. -- All examples in the documentation pass a local variable without further comment, so not entirely clear what this does other than set the output datatype.) dbms_sql.define_column(l_cursor_handle, 1, l_value, 4000); -- Now go through values array binding actual values to :bn variables in the cursor (similar to USING clause of EXECUTE IMMEDIATE) for i in t_values.first .. t_values.last loop dbms_sql.bind_variable(l_cursor_handle, ':b'||i, t_values(i)); dbms_output.put_line('Bound :b'||i || ' as ' || t_values(i)); end loop; -- Open the cursor and fetch the result (no loop here because we are expecting a single-row result): l_rows_fetched := dbms_sql.execute_and_fetch(l_cursor_handle); -- 'Returns value of the cursor element for a given position in a cursor' -- Copy the value of column 1 to variable l_value (has to match -- dbms_sql.column_value(l_cursor_handle, 1, l_value); dbms_sql.column_value(l_cursor_handle, 1, l_value); dbms_output.put_line('Result = ''' || l_value || ''''); dbms_sql.close_cursor(l_cursor_handle); end;
Hope that helps...
-
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.
-
Storing the values of execute immediate instruction.
Hi all
I execute immediate statement that gets several rows at a time. I need to save this picture and print. Pleas elte the syntax me to use an array or temp table in oracle. See the code provided below.
Thank you...
declare
v_ct varchar2 (290);
v_sqlcode number: = 0;
v_stmt varchar2 (2900);
r_tbl VARCHAR2 (150);
cursor c_tbl is
Select the table TABLE_NAME from DBA_TABLES where OWNER = 'ted '.
Start
Open c_tbl;
loop
extract the c_tbl in r_tbl;
When the output c_tbl % notfound;
v_stmt: = 'select NULLABLE from DBA_TAB_COLUMNS WHERE TABLE_NAME =' | " ' || r_tbl | " ' |' and COLUMN_NAME in (select column_name from DBA_IND_COLUMNS where INDEX_NAME = (select 'INDEX_NAME' from DBA_INDEXES where TABLE_NAME =' | ")) ' || r_tbl | " ((' |' and UNIQUENESS = "UNIQUE"))';
run immediately v_stmt in v_ct;
v_sqlcode: = SQLCODE;
If v_sqlcode = 0
then dbms_output.put_line (v_ct);
end if;
end loop;
close c_tbl;
end;Your immediate question seems to be answered.
I would like to point out a few problems with your code, however.
First of all, it seems pointless to use dynamic SQL here - you can use a static SQL statement to get the same data. You can use static SQL, you should.
Secondly, if you want to use dynamic SQL statements, you should really use bind variables. Something like
v_stmt := 'select NULLABLE from DBA_TAB_COLUMNS WHERE TABLE_NAME = :1 and COLUMN_NAME in (select column_name from DBA_IND_COLUMNS where INDEX_NAME= (select "INDEX_NAME" from DBA_INDEXES where TABLE_NAME =':2 and UNIQUENESS =''UNIQUE''))'; execute immediate v_stmt bulk collect into v_ct using r_tbl, r_tbl;
Third, your deepest query is to select the literal 'INDEX_NAME' rather than the name of the actual index. Unless you happen to have an index named INDEX_NAME, who is likely to be a mistake.
v_stmt := 'select NULLABLE from DBA_TAB_COLUMNS WHERE TABLE_NAME = :1 and COLUMN_NAME in (select column_name from DBA_IND_COLUMNS where INDEX_NAME= (select INDEX_NAME from DBA_INDEXES where TABLE_NAME =':2 and UNIQUENESS =''UNIQUE''))'; execute immediate v_stmt bulk collect into v_ct using r_tbl, r_tbl;
Fourth, unless you can be sure that the table has a unique index, your query will return an error because the deepest returns multiple lines.
v_stmt := 'select NULLABLE from DBA_TAB_COLUMNS WHERE TABLE_NAME = :1 and COLUMN_NAME in (select column_name from DBA_IND_COLUMNS where INDEX_NAME in (select INDEX_NAME from DBA_INDEXES where TABLE_NAME =':2 and UNIQUENESS =''UNIQUE''))'; execute immediate v_stmt bulk collect into v_ct using r_tbl, r_tbl;
Justin
Published by: Justin Cave on August 5, 2011 23:28
-
Hai I want dynamic query for framing
HORN must be updated using dynamic queryCREATE TABLE PI4 (COR varchar2(1),dep varchar2(3),cod_rc varchar2(2)) insert into pi4(dep,cod_rc) values('A01','5C') insert into pi4(dep,cod_rc) values('DAC','5L')
as below
sql_stament is that the variable must be set to above code.UPDATE PI4 SET PI4.COR = ’O’ WHERE PI4.DEP IN ('A01','A02','A03') OR ( PI4. dep IN ('DAC','FTS','G12') AND PI4.COD_RC = '5L' )
Pls help
SCheck below the code where the CO value is assigned dynamically to a p_cor variable
DECLARE v_sql_statement VARCHAR2(32767) := NULL; p_cor VARCHAR2(10) := 'O'; BEGIN v_sql_statement := 'UPDATE PI4 SET PI4.COR ='||p_cor||' WHERE (PI4.DEP IN (''A01'',''A02'',''A03'') OR (PI4. dep IN (''DAC'',''FTS'',''G12'') AND PI4.COD_RC = ''5L''))'; EXECUTE IMMEDIATE v_sql_statement; END;
If you do not need dynamic query to perform the update:
See the code below:
DECLARE p_cor VARCHAR2(10) := 'O'; BEGIN UPDATE PI4 SET PI4.COR =p_cor WHERE PI4.DEP IN ('A01','A02','A03') OR (PI4. dep IN ('DAC','FTS','G12') AND PI4.COD_RC = '5L')); END;
Published by: AP on July 27, 2010 03:39
Published by: AP on July 27, 2010 04:51: end tag code included
-
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
-
dynamic query send multiple values dynamically
Hi all
I'm trying to run a dynamic query. Is it possible to send dynamically deptno and ename?
declare
v_sql varchar2 (4000);
v_resultCol varchar2 (100): = 'ename ';
v_col varchar2 (100): = "deptno";
number of v_num: = 30;
number of v_count;
Start
-v_sql: = 'select'. : v_resultCol |' from emp where ' | : v_col | '= '|| : v_num;
v_sql: = ' select count (ename) from emp where deptno =: a ';
run immediately v_sql in v_count using v_num;
dbms_output.put_line (v_count);
end;
/
Thank youYou can not choose a column dynamically by passing as a bind value because the binding for the name of the column would be simply considered as a string, in this case a count of the a non-empty string would be the same as count (*), a separate of a non-empty string account would be 1, and an empty string County 0 , which is not a lot get you if you try to count the number of records with non-null to a column values.
As a dynamically selected column you must can dynamicaly would build the SQL, but opens you up to SQL Injection attacks:
DECLARE v_col varchar2(100) := 'null)+max((select empno from emp where ename=''CLARK'')'; v_sql VARCHAR2(4000); v_count number; begin v_sql := 'select count('||v_col||') from emp where deptno = :a'; execute immediate v_sql into v_count using 30; dbms_output.put_line(v_count); end; /
a better approach would be to first make sure that the value of v_col is actually a column in the table:
DECLARE v_col varchar2(100) := 'null)+max((select empno from emp where ename=''CLARK'')'; v_sql VARCHAR2(4000); v_count number; begin begin select column_name into v_col from all_tab_cols where owner = 'SCOTT' and table_name='EMP' and column_name = v_col; exception when NO_DATA_FOUND then v_col := '*'; end; v_sql := 'select count('||v_col||') from scott.emp where deptno = :a'; execute immediate v_sql into v_count using 30; dbms_output.put_line(v_count); end; /
Use a box or decode statement in your selection to determine which column to use based on a variable binding:
DECLARE v_col varchar2(100) := 'COMM'; v_sql VARCHAR2(4000); v_count number; begin v_sql := 'select count(case :col_name '|| 'when ''ENAME'' then ename '|| 'when ''COMM'' then to_char(COMM) '|| 'end) '|| 'from scott.emp where deptno = :a'; execute immediate v_sql into v_count using v_col, 30; dbms_output.put_line(v_count); end; /
Published by: Sentinel on October 16, 2008 14:48
-
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
-
Maximum size of EXECUTE IMMEDIATE
Hellou everyone,
I would like to ask if there is a way how to run the dynamic SQL CREATE or VIEW of REPLACE command more than 32 KB of wholesale. Because the EXECUTE IMMEDIATE statement can run up to 32 k (greater length of variable plsql chain) chain
Is there a way how children Ridge or replace the command?
Or I need to revrite using DBMS_SQL.
Thank you.
(a) WHY? If you want to dynamically create views? This is not recommended because your application/database should be designed at the time of the design, not running.
(b) If you are on 11 g, you can use the CLOB datatype with immediate execution, so there is no 32 K limit.
(c) If you are before 11 g you need to update, but if you can't then the following example is how you do it using DBMS_SQL.
SQL > declare
2 v_large_sql clob.
3 v_num number: = 0;
number of v_upperbound 4;
5 v_sql dbms_sql.varchar2s;
6 whole v_cur;
number of v_ret 7;
8 v_join_sql varchar2 (100): = ' crΘer view vw_tmp as ";
9 start
10. build a very big SQL statement in the CLOB
11 loop
12 v_large_sql: = v_large_sql | v_join_sql | "select" the number of this line is: ' | TO_CHAR (v_num, 'fm0999999') | " "as col1 from dual;"
13 v_join_sql: = "union all";
14 v_num: = v_num + 1;
15 exit when dbms_lob.getlength (v_large_sql) > 40000 or v_num > 800;
16 end loop;
17 dbms_output.put_line (' length :'||) DBMS_LOB. GetLength (v_large_sql));
18 dbms_output.put_line ('Num :'|| v_num);
19-
20. now divide this big SQL statement into pieces of 256 characters and put in table VARCHAR2S
21 v_upperbound: = ceil ((v_large_sql) dbms_lob.getlength / 256);
22 because me at 1.v_upperbound
23 loop
24 v_sql (i): = dbms_lob.substr (v_large_sql
25, 256 - amount
26, ((i-1) * 256) + 1 - offset
27 );
28 end of loop;
29-
30. now analyze and execute the SQL statement
31 v_cur: = dbms_sql.open_cursor;
32 dbms_sql.parse (v_cur, v_sql, 1, v_upperbound, dbms_sql.native, false);
33 v_ret: = dbms_sql.execute (v_cur);
34 dbms_output.put_line ("' view created");
35 dbms_sql.close_cursor (v_cur);
36 end;
37.
Length: 40015
NUM:548
View created
PL/SQL procedure successfully completed.
SQL > select count (*) in the vw_tmp;
COUNT (*)
----------
548
SQL > select * from vw_tmp where rownum<=>=>
COL1
-----------------------------------
The number of this line is: 0000000
The number of this line is: 0000001
The number of this line is: 0000002
The number of this line is: 0000003
The number of this line is: 0000004
The number of this line is: 0000005
The number of this line is: 0000006
The number of this line is: 0000007
The number of this line is: 0000008
The number of this line is: 0000009
10 selected lines.
SQL >
-
Executions of dynamic query taking a long time
Dear all,
During the race the query below, it takes a long time to execute.
[code]
DECLARE
statement varchar2 (1000);
BEGIN
FOR c1 IN (SELECT tenant_id FROM tenant WHERE tenant_id! = 0)
LOOP
FOR c2 IN (SELECT alignment_id FROM customer_alignment WHERE affiliation_id IS NULL AND tenant_id = c1.tenant_id)
LOOP
FOR c3 IN (SELECT vw.affiliation_id, vw.customer_id, vw.alignment_id, ca.customer_alignment_id
OF ca_primary_address_vw vw, ca customer_alignment, WHERE ca.affiliation_id IS NULL
AND ca.alignment_id = c2.alignment_id
AND ca.customer_id = vw.customer_id
AND ca.alignment_id = vw.alignment_id
)
LOOP
Statement: = "UPDATE customer_alignment SET affiliation_id ='|" C3.affiliation_id | |' where customer_alignment_id =' | C3.customer_alignment_id;
execute immediate (statement);
END LOOP;
END LOOP;
END LOOP;
END;
[/ code]
Can you please help me on this issue.
I use the version of Oracle 11 g.
Thanks a lot for your help.
Miriam.
Never perform a treatment by data line. SET the computer base. Direct use of the joints and cut single SQL. Your condition can be obtained by using the merge statement.
merge into customer_alignment c using ( select vw.affiliation_id , ca.customer_alignment_id from customer_alignment c join tenant t on t.tenent_id = c.tenent_id and t.tenent_id != 0 and c.affiliation_id is null join customer_alignment ca on ca.alignment_id = c.alignment_id and ca.affiliation_id is null join ca_primary_address_vw vw on ca.customer_id = vw.customer_id and ca.alignment_id = vw.alignment_id ) t on ( c.customer_alignment_id = t.customer_alignment_id ) when matched then update set c.affiliation_id = t.affiliation_id;
-
I wonder why the dynamic value query is executed before the boot process? Logically, it makes sense to run after them.
For example, I test a stored procedure that is supposed to delete a record, and I'd like to create this test report should be deleted as part of the startup process before execution of the stored procedure call to delete this test record. Apparently the dynamic query of value not returns not the test report in as long as the query parameter to call the stored procedure under test, which makes me think that is executed before the startup process of design...
Please advise...
Thank you
Val
As this thread does no traction/attention of the team of SQL Developer for a while, I had to submit a request for formal improvement on metalink:
RE: 19834977 - IN THE UNIT TEST REQUEST TO ALLOW TO CHANGE THE ORDER OF EXECUTION OF THE START OF THE PROCESS
Thank you
Val
-
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.
-
Need help tuninng a dynamic query
Hello
We have following question
Select s.* s bose_sites.registration_crm_serial_number
R2 bose_sites.registration_crm_serial_number left on join
s.serialnr = r2.shipserialnr
where s.shipserialnr is null
and (r2.serialnr = ' 052378332000138AE' or (s.serialnr =)
(( "052378332000138AE" and s.shipserialnr is null))
which takes about 40 seconds to execute with the values highlighted in bold being dynamic values
If anyone can help reduce the run time on this request.
There are indexes created on table
INDEX_NAME INDEX_TYPE
------------------------------ ---------------------------
IDX_REG_CRM_SER_NUM_CPC NORMAL
IDX_REG_CRM_SER_NUM_SHIPDATE NORMAL
IDX_REG_CRM_SER_NUM_PARENT_ID NORMAL
IDX_REG_CRM_SER_NUM_ID NORMAL
is it possible that we can capture a dynamic query using DBMS_SQLTUNE
all suggestions will be useful.
Hello
You have one table here
-------------------------------------------------------------------------------
Schema name: SYS
SQL ID: cf2875zz4q4nd
SQL text: select s.* bose_sites.registration_crm_serial_number s
R2 bose_sites.registration_crm_serial_number left on join
s.serialnr = r2.shipserialnr
where s.shipserialnr is null
and (r2.serialnr = '052378332000138AE' or (s.serialnr =
S.shipserialnr and "052378332000138AE" is null))
-------------------------------------------------------------------------------
then why don't you go for a simple query
HTH
Maybe you are looking for
-
I'm the double images on my HP S2031 monitor. How can I fix it?
-
dv9260nr volume high/low lights don't light up
I have recently upgraded to windows 8, then 8.1 pro, now the volume up/down lights do not work, the muted light works. I can change the volume up/down work that just the lights do not work. I have the pc hp pavilion dv9260nr laptop.
-
expresscache for the mSata SSD problems
I have a 16 GB on a T530 mSata reader. Rebuilt my machine and I am trying to run expresscache. Deleted the partition on disk mSata and installed the expresscache software from this link: http://support.Lenovo.com/en_CA/downloads/detail.page?docid=DS0
-
screen flashing on off
-
Volume stuck at 0% on Z3 after update 23.4.A.1.200
Just upgrade to a new software but it get worse... my Z3 sound was disable permanent and can not able to... .whatever when I increase the volume it still stand, always 0% is unable to make it 100% max... are any solution for it... I don't want my pho