Execute immediate with using clause to pass column name dynamically
Hai,
Is there any way using execute immeidate to pass the column name dynamically. I used to pass the column value as dynamic with the help of "Using clause" . But if i use to pass column name, it is giving numberic error at run time. Eg,. for testing has been given below.
1. Column value as dynamic, which is working correctly.
create or replace function testexeimm (acctnum char)
return number as
acctbal number;
begin
execute immediate 'select balance from acct_master where acct_no=:a' into acctbal using acctnum;
return acctbal;
end;
2. Column name as dynamic which is not working
create or replace function testexeimm (colnam char)
return char as
acctbal char;
begin
execute immediate 'select :a from ch_acct_mast where rownum=1' into acctbal using colnam;
return acctbal;
end;
Any help in this regard will be highly appericated.
Regards
Sridhar
So the variable has to be numeric too:
create or replace function testexeimm (colnam char)
return number as
acctbal number;
begin
execute immediate 'select '|||colnam||' from ch_acct_mast where rownum=1' into acctbal;
return acctbal;
end;Max
http://oracleitalia.wordpress.com
Similar Messages
-
Pass column names dynamically in report
Hello experts,
I am creating an ALV report where i need to pass the column names dynamically. if the current month is March and year is 2009. the column names should be as below
March 2009 April 2009 May 2009 June 2009 ..............till Feb 2010 (total columns are 12)
if the current month is June and year is 2010 it shoud be displayed as
June 2010 July 2010 Aug 2010................till May2011
I am actually calculating the forecast qty for each month and displaying in a report from the current month.
How to write field catelog for this requirement?
Thanks in Advance.
Rajesh.I think in the fieldcat, at the time of declearation, you can alter the field description depending on the present month.
If present_month = January
wa_fieldcat-reptext_ddic = 'January'. " Field description
elseif present month = February.
wa_fieldcat-reptext_ddic = 'February'. " Field description
on so on.....................
Kuntal -
Execute immediate with a string longer than 32767
hello,
is there a possibility to make an execute immediate with a statement, that is longer than the 32767 max. bytes for varchar`s? We get in a CLOB SQL-Statements with data that are longer than 32767. It would be easy if it were possible to make an execute immediate but because of the length limit it seems impossible. Does anybody know an alternative way to execute the sql-statements in the CLOB-Column?
Anna6 execute immediate to_char(sql_text) into var_num;As a matter of interest have you tried this with a CLOB that is bigger than 32K?
When I try it the PL/SQL limit gets enforced:
SQL> declare
2 c clob;
3 begin
4 dbms_lob.createtemporary(c, true);
5 dbms_lob.writeappend(c, 43, 'CREATE OR REPLACE PROCEDURE big_p AS BEGIN ');
6 for i in 1..800 loop
7 dbms_lob.writeappend(c, 6, 'null;'||chr(10));
8 end loop;
9 dbms_lob.writeappend(c, 4, 'END;');
10 execute immediate to_char(c);
11 end;
12 /
PL/SQL procedure successfully completed.
SQL> declare
2 c clob;
3 begin
4 dbms_lob.createtemporary(c, true);
5 dbms_lob.writeappend(c, 43, 'CREATE OR REPLACE PROCEDURE big_p AS BEGIN ');
6 for i in 1..8000 loop
7 dbms_lob.writeappend(c, 6, 'null;'||chr(10));
8 end loop;
9 dbms_lob.writeappend(c, 4, 'END;');
10 execute immediate to_char(c);
11 end;
12 /
declare
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error
ORA-06512: at line 10
SQL> Cheers, APC -
How to pass column name at run time in function.
how to pass column name at run time in function as parameter.
thank in advance
pramod patelHello,
Using dynamic sql you can pass column name to function. well I am not getting what you really want to do ? Please write in more detail. By the way I am providing one example here. see it uses dynamic sql.
Create or replace function fun_updtest (p_columnname_varchar2 in varchar2,
p_value_number in number)
return number is
v_stmt varchar2(500);
begin
v_stmt := 'update emp
set '||p_columnname_varchar2||' = '||to_char(p_value_number)||'
where empno = 7369';
execute immediate v_stmt;
return 0;
commit;
end;
call to this function can be like this..
declare
v_number number;
begin
v_number := fun_updtest('SAL',5000);
end;
Adinath Kamode -
Pass column-name as a parameter to reports
Hello,
the code below calls a report. But now I want to sort the rows in the report. For example I have a text-item in my form-modul. If I type a column-name and press the button then the rows should be sorted in the report. Is it possible tp pass column-names as parameter to reports?
DECLARE
repid REPORT_OBJECT;
v_rep VARCHAR2(100);
rep_status VARCHAR2(20);
BEGIN
repid := find_report_object('STATIONSTOPOLOGIE');
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_EXECUTION_MODE,RUNTIME);
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_COMM_MODE,SYNCHRONOUS);
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_DESTYPE,CACHE);
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_DESFORMAT,'html');
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_SERVER,'rep_oracle-dev');
-- SET_REPORT_OBJECT_PROPERTY(repid,REPORT_OTHER,'paramform=no pdeptno='||:dept.deptno);
v_rep := RUN_REPORT_OBJECT(repid);
rep_status := REPORT_OBJECT_STATUS(v_rep);
WHILE rep_status in ('RUNNING','OPENING_REPORT','ENQUEUED')
LOOP
rep_status := report_object_status(v_rep);
END LOOP;
IF rep_status = 'FINISHED' THEN
WEB.SHOW_DOCUMENT('http://oracle-dev:8888/reports/rwservlet/getjobid'||
substr(v_rep,instr(v_rep,'_',-1)+1)||'?'||'server=rep_oracle-dev','_blank');
ELSE
message('Error when running report');
END IF;
END;Hi,
the work has been done in reports. You can use a lexical parameter in reports to add a condition for sorting to the query like:
select .. from .. where ... &p_order.
Then add another parameter to the report (for example p_param). Fill p_param via your interface in forms (SET_REPORT_OBJECT_PROPERTY(repid,REPORT_OTHER, ....) with the column name. Then build a report trigger like:
if :p_param is null
then
:p_order:= null;
else
:p_order:= 'order by '||:p_param;
end if;
But have a look, that p_param can only get correct values.
Rainer -
How to pass column name as a values from one page to another
hi
i have created a report(pivot) from a table
SQL> SELECT * FROM T;
C1 C2 C3 D SEQ
A AA 2 1
A AB 3 2
A AC 2 3
B AB 5 4
B AC 6 5
SQL> SELECT C1
2 ,NVL(MAX(CASE WHEN C2='AA' THEN C3 END),'') AA
3 ,NVL(MAX(CASE WHEN C2='AB' THEN C3 END),'') AB
4 ,NVL(MAX(CASE WHEN C2='AC' THEN C3 END),'') AC
5 ,SUM(C3) FROM T GROUP BY C1;
C1 AA AB AC SUM(C3)
A 2 3 2 7
B 5 6 11
SQL>
my requirement in Apex is like this(reverse)
eg-
when i click on cell values '2' then,it should return
C1 C2 C3 D SEQ
A AA 2 1
{quote}how to pass column name as a values from one page to another
for example i have to pass 'c2' as a value to next page{quote}for report pivot you can reffer below link
Report
Amuthanks for your reply
i 'm doing what exactly you mention here .
my problem here is
i have 15 columns
i am executing a query based on the values of the column(column name) in the target page
1)here i am passing(all) the column values to the next page-but i want to pass only one column values(column name)
when i click on any cell of that column
OR
2)i can pass all column name to target page -there(in the target page) i can filter out
i think option 1 would good if you filter out the unwanted columns
Regards
Amul -
Passing Column name(String) in setting CallableStatement
Is it possible to pass Column Name while setting the CallableStatement while using drivers for JDK 1.4, if yes how do I do it.
Thanks!You don't necessarily need to use dynamic SQL;
create or replace procedure p
p_a number default null,
p_b number default null,
p_c number default null
) is
begin
insert into t
values
(p_a, p_b, p_c);
end;
Procedure createdUsing named notation;
exec p(p_a =>1, p_c => 3);
PL/SQL procedure successfully completed
exec p(p_b =>20, p_c => 30);
PL/SQL procedure successfully completedPassing NULLs;
exec p(4, null, 6);
PL/SQL procedure successfully completed
exec p(40, 50, null);
PL/SQL procedure successfully completed
select * from t;
A B C
1 3
20 30
4 6
40 50 -
How to pass column name in slect statement in query
hi,
i want to make a report where in query select statement using variable as a column name. but its not working plz guide me how can i do this.
i have created a function which return column name through variable & that variable i want to to use in select statement
select :m1 from table1;
regardsHi,
Create a user parameter (say P_field), and assign a valid field name as initial value (say NAME), And In the Query, write
SELECT CODE, &P_field FN_FIELD FROM <table_name> WHERE <condition>And in the BEFORE PARAMETER FORM Trigger under the Report Triggers, write,
function BeforePForm return boolean is
begin
:P_field := <your_function_call>;
return (TRUE);
end;And use that FN_FIELD field in the report.
Hope this will clear your issue.
Regards,
Manu. -
Column names dynamically in Dynamic SQL
Hi All,
We can use Table Name dynamically in Dynamic SQL.
Can we use column names dynamically?
Thanks,
UmasankarHi Todd Barry,
Thanks lot for your immediate response.
Could you please tell me, how can we use column name dynamically?
I tried but, I didnt get the correct result.
Here is my coding.
Declare
SQL_String Varchar2(2000);
Category varchar2(50):= 'ELECTRO_PLATING_413';
Type ConstCurTyp is REF CURSOR;
Const_Cv ConstCurTyp;
vLab_name varchar2(80);
Begin
Execute Immediate 'Truncate table tto_lab_names';
dbms_output.put_line(category);
dbms_output.put_line(' ');
SQL_String := 'Select lab_name from tto_constituents where :category is not null';
Open Const_cv for SQL_String using category;
Loop
Fetch Const_Cv into vLab_Name;
Exit When Const_Cv%NotFound;
Insert into lab_names values(vLab_name);
End Loop;
Commit;
Close Const_Cv;
End;
I am getting all the lab_names as result.
Thanks,
Umasankar
null -
Getting the column names dynamically
hi gurus,
i have list item populated with many table names from a schema.
i have grid in oracle forms 10g and i want to fill the grid with the data that should come at least four/more columns.
i want to fire the list change trigger when each time any one table name is selected/changed.
how can i get the column names dynamically
please give me the step by step process
for filling the grid..hi ,
by getting column names dynamically i am creating a record group
using this below query
SELECT COLUMN_NAME, DATA_TYPE FROM USER_TAB_COLUMNS WHERE TABLE_NAME = <table_name>
but when timestamp datatype is there then it is showing error cannot create group groupname
but the record group is running successful on varchar2,number,date dayatypes
if any alternative is there i mean to say without using record group then tell me or where is the problem -
HTML DB Chang column name dynamically
Can I change the column name dynamically. for instance
Today run my apply it will display the title of the columns this
Column title: Wed Thu Fri
04/22/05 04/23/05 04/24/05
Data 45 34 34
54 56 23
Next week
Column title: Wed Thu Fri
05/03/05 05/04/05 05/05/05
45 34 34
54 56 23
The date Change Dynamically?Go to your report and open report atributes. There you can choose the options for column names. Click on the pl/sql function and type this in:
DECLARE v varchar2(20);
begin
v := to_char(SYSDATE, 'dd.mm.yy')||':'||to_char(SYSDATE+1, 'dd.mm.yy');
RETURN v;
END;
This will set the first two columns to the specified dates. You can use this as one option.
If the case is more complicated than you can do the following:
1. create a number of items on your page - hidden
2. compute the items with values once the page is loaded
3. use item substitute string &ITEM. in the column name, which will use the item value as a name for the column.
This way you are more flexible since you can use the item values for some columns only and have the other columns hardcoded.
Denes Kubicek -
Using EXECUTE IMMEDIATE with XML
Database version : 10.2.0.3.0 - 64bi
Hi All,
I have a xml which is stored in a table, xmltype column.
i have target insert tables whose column names and xml nodes are same.
using all_tab_columns table i will generate columns to be passed to xmltable.
all these data i will store in variables and finally pass to xmltable as below
just want to know using execute immediate is good to use in XML?
SQL_STMT := 'insert into '||table_name|| ' ( '||V_COLUMN_NAME||')';
SQL_STMT := SQL_STMT ||' SELECT ' ||V_XTAB_COLUMN_NAME ||
' FROM TO_XML,
XMLTABLE(' ||v_xpath||
'PASSING XML_VALUE
columns ' || V_COLUMNS_DATA_TYPE
||') XTAB
WHERE Seq_NO = ' || P_SEQUENCE_NO ;
EXECUTE IMMEDIATE SQL_STMT ;
Thanks and Regards,
Rubu1) is it OK? As I stated above, it can be made to work. It would not be my first choice, but then none of us here know the full details as well as you do so maybe there is a compelling reason to use dynamic SQL.
Here is the documentation for [url http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/executeimmediate_statement.htm#LNPLS01317]EXECUTE IMMEDIATE.
Actually now I finally realize your XML resides in table TO_XML so that means you won't be putting the actual XML into the shared pool via an incorrectly written dynamic SQL statement at least. That is what Odie and I were first concerned about with dynamic SQL usage, that the XML would be hard-coded into your SQL_STMT variable. You are simply changing the columns (in 3 locations). With that setup, you have no need for (nor can use) bind variables. The overall issue of dynamic SQL being slightly slower than static SQL still exists as the SQL statement will first have to be parsed and validated.
A larger issue in terms of performance is how 10.2 handles XMLTypes. If the underlying XML is large, XMLTable performance degrades quickly. Options around this are to parse the XML in PL/SQL or to upgrade to some version of 11 and use SECUREFILE BINARY XML as the underlying storage structure for the TO_XML.XML_VALUE column. -
Using EXECUTE IMMEDIATE with Create Table SQL Statement not working
Hi ,
I am all the privileges given from the SYSTEM user , but still i am not able to create a table under procedure . Please see these and advice.
create or replace procedure sp_dummy as
begin
Execute Immediate 'Create table Dummy99_99 (Dummy_Field number)';
end;
even i tried this way also
create or replace PROCEDURE clearing_Practise(p_file_id in varchar2, p_country in VARCHAR2,p_mapId in VARCHAR2)
AUTHID CURRENT_USER AS
strStatusCode VARCHAR2(6);
BEGIN
EXECUTE IMMEDIATE 'create table bonus(name varchar2(50))';
commit;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('ERROR Creating Table');
END ;William Robertson wrote:
Since the syntax is correct, my guess is you do not have CREATE TABLE system privilege granted directly to your account. A common scenario is that you have this privilege granted indirectly via a role, allowing you to create tables on the command line, but stored PL/SQL is stricter and requires a direct grant and therefore the procedure fails with 'insufficient privileges'.A bit like he's already been told on his first thread...
Using of Execute Immediate in Oracle PLSQL
Generally you would not create tables from stored PL/SQL. Also as you have found out, it's best not to hide exceptions with 'WHEN OTHERS THEN [some message which gives less detail than the one generated by Oracle]'.Again like he was told on the other thread.
There's just no telling some people eh! :) -
Using EXECUTE IMMEDIATE with an NVARCHAR2 parameter
Hi everyone,
In the system I'm working on, my stored procedure receives an NVARCHAR2 parameter which contains a complete SQL statement. e.g.
'UPDATE SomeTable SET SomeTable.Value = 0'
I want to be able to execute this dynamically, using EXECUTE IMMEDIATE, but I've encountered a problem where this is apparently not supported (because the string is Unicode).
It is crucial that the string stays as Unicode, because of the data that may possible be baked into the SQL statement, and so casting it off to a VARCHAR2, or changing the parameter type, is not acceptable.
Is there a work-around for this issue? Even if it has a performance hit, it will be better than nothing :)
I've tried something like this;
declare
myVal VARCHAR2(256);
begin
myVal := 'UPDATE SomeTable SET SomeTable.Value = 2';
execute immediate 'BEGIN :x; END;' using myVal;
end;
But I get an error;
"bind variable 'X' not allowed in this context"
Has anyone any ideas?
Thanks for your help,
Chrisuhmm, I'm not sure if this is a valid testcase. the string could still be converted into ascii or we8iso8859p1 or whatever 1byte-character set you want.
What I think you should test is a two-byte character as part of SQL, PL/SQL resp. as in SQL all words are defined you can not enlarge it with a 2byte word. But in PL/SQL you could define your own variable. If this variable name itself contains a 2byte character, I guess it will fail.
(I have used l_vär and l_vér as variable names in my example)
Message was edited by:
Leo Mannhart
According to the PL/SQL Reference Guide PL/SQL consists of:
PL/SQL programs are written as lines of text using a specific set of characters:
Upper- and lower-case letters A .. Z and a .. z
Numerals 0 .. 9
Symbols ( ) + - * / < > = ! ~ ^ ; : . ' @ % , " # $ & _ | { } ? [ ]
Tabs, spaces, and carriage returns
This seems no longer up-to-date as I could define a variable l_vär for instance as long as I use a 1byte char set (like iso8859p1). The same variable name as a n-byte char set like UTF-8 will give an error as my example shows. -
Difficulties with execute immediate and 'in' clause
Hello all,
I have some SQL that will have to run dynamically, which I can get to work by building a long statement, then getting it hard-parsed
example:
sql_stmt VARCHAR2(256) :=
'select distinct some_id from table1
where lname = ''Smith''
and fname in(''Joe'', ''Joey'', ''Joseph'')
and ssn = 111223333';
execute immediate sql_stmt into new_id_value;
the above state executes fine and returns the desired result.
HOWEVER, when I try to use bind variables (this statement will be called several million times), I get a no rows returned error.
I use bind variables as follows:
sql_stmt VARCHAR2(256);
vlname VARCHAR2(50) := 'Smith';
vssn NUMBER := 111223333;
vfname VARCHAR2(100) := '''Joe'', ''Joey'', ''Joseph''';
sql_stmt :=
'select distinct some_id from table1
where lname = :vlname
and fname in(:vfname)
and ssn = :vssn;
execute immediate sql_stmt into new_id_value
using vlname, vfname, vssn;
I know the issue involves the 'in' clause because I've substituted hardcoded values in and it has worked.
any suggestions?Well maybe its all a little confusing, does an end to end demo make it any clearer Mr Bidness?
SQL> create or replace type tabstr_t as table of varchar2(4000)
2 /
Type created.
SQL> create or replace function tabstr (
2 p_str in varchar2,
3 p_sep in varchar2 default ','
4 )
5 return tabstr_t
6 is
7 l_str long := p_str || p_sep;
8 l_tabstr tabstr_t := tabstr_t();
9 begin
10 while l_str is not null loop
11 l_tabstr.extend(1);
12 l_tabstr(l_tabstr.count) := rtrim(substr(
13 l_str,1,instr(l_str,p_sep)),p_sep);
14 l_str := substr(l_str,instr(l_str,p_sep)+1);
15 end loop;
16 return l_tabstr;
17 end;
18 /
Function created.
SQL> var s varchar2(100)
SQL> exec :s := 'king,turner,ward'
PL/SQL procedure successfully completed.
SQL> select ename, sal from emp
2 where ename in (
3 select upper(column_value) from
4 table(cast(tabstr(:s) as tabstr_t))
5 );
ENAME SAL
KING 5000
TURNER 1500
WARD 1250
SQL> exec :s := 'jones,blake,clark'
PL/SQL procedure successfully completed.
SQL> /
ENAME SAL
BLAKE 2850
CLARK 2450
JONES 2975
Maybe you are looking for
-
How can i assign a Javascript variable to JSP variable
Hi guys how i can assign a javascript variable to jsp expression., e.g., <input type = "button" value = "Add more" onclick = "return submitform1('<%=s%>')"> function submitform1(String s) var s1 = s document.form1.action = "../intimation.do";
-
A question from a new Blackberry user
Hi all, I will be going for the 9360, but unfortunatly, my operator doesn't seem to support the Blackberry phones. There are a few others that do that here, but then you need to subscribe to the Blackberry service. Does anyone know if I will be able
-
Portege R100: recovery issue - Error partioning drive
My old portege r100 is not working at all. I tried to use the product recovery cd rom but during the recovery process the following message apears: Error partioning drive! What does it mean? What can i do to overcome it? Thanks in advance Yannis
-
I'm using Encore 1.5 and just gave a client a slide show encoded from Premiere 1.5.1. The DVD played fine on my Panasonic player but the client reported problems with both DVDs he got. I tested on a different player (Sony) and sure enough, it would n
-
How to use warn about changes property with page menus
how to use warn about changes property with page menus