Passing column names in as a variable
I know in SQL server there is a way you can pass a variable in to the procedure and put that variable as a column name, Oracle doesn't seem to pick the variable up as a column name. Can anyone show me how with a simple select statement
bgulcu wrote:
I'm having a similar problem. I didn't want to open up another thread since it's related.
Problem:
I have 107 columns on a table. The number of columns increases almost every month.
I have an array of columns, which I have gathered from the db. I need to pivot the whole table, but due to indefinite number of columns, it had to be dynamic sql.
Premature Solution:
Right now, the solution that we have runs through the array of columns and uses dynamic sql to query for every single cell to get the value. Considering that we have 1000+ rows on this table, in order to do the transformation, we send 107000+ queries which is just ugly. It works, but I don't feel comfortable about it.
Solution:
Since we are able to get a single row of 107 columns, why don't we gather the whole row, and get the information out from it instead of tiring up the server? The indefinite number of columns is a problem, but we have the whole list in an array, lets say feature_array. So what I want to do is to get the date from the row, ie myRow.feature_array(1), myRow.feature_array(2), ..., myRow.feature_array(i).
I just learned that it's not possible to do it (from this thread).You just misunderstood the contents of this thread. What we said is that you can't do such things with static SQL, so you need to use dynamic sql.
What has also been told was that using dynamic sql for such a purpose is just the result of a bad design. Use a relational table as a bi-dimensional array is not right, it is possible but it's not right for the simple reason that you need a procedural language to process the dynamic SQL that is needed to retrieve data.
Probably on database programming there's only a rule of thumb and it says: "Maximize SQL minimize PL/SQL" (or any procedural language like java). The only exceptions to keep code in procedural language must depend on a complexity reduction determined by its use. In your case there is no complexity reduction determined by the use of a procedural language.
In the thread on this link(Schema integrity I discussed about how to implement a solution you can use as alternative for submitting queries with variable column names.
If you introduce parameter management in your model probably you could have your problem solved with the use of only a single SQL statement. And that may be a good designed model.
Bye Alessandro
Similar Messages
-
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
SridharSo 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 -
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 -
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 -
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. -
Passing column name dianamically in an insert query
i have a procedure hwre i used an inser query.eg
insert into table (a,b,c) values(...)
i need to pass a,b,c ie column names as parameter.depending upon the condition the column name will change.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 -
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 -
Passing column name inside an update query
Hi,
To checking if there any number field present in 'ID' column, I am using the follwing query.
UPDATE table1
SET COL1 = 'Y'
WHERE 0 = (
SELECT CASE
WHEN ID IS NULL THEN 1
WHEN REGEXP_LIKE(
ID,
'^( *)(\+|-)?((\d*[' || SUBSTR(VALUE,1,1) || ']?\d+)|(\d+[' ||
SUBSTR(VALUE,1,1) || ']?\d*)){1}(e(\+|-)?\d+)?(f|d)?$','i'
) THEN 1
ELSE 0
END
FROM NLS_SESSION_PARAMETERS
WHERE PARAMETER = 'NLS_NUMERIC_CHARACTERS'
Now I need to extend this query to check 10 more columns from table1. How can modify the above query to pass that 10 columns the above query.
ThanksGot it ! Then again your syntax is incorrect... You should use correlated subquerry
UPDATE table1
SET COL1 = 'Y'
WHERE 0 =
(SELECT CASE
WHEN ID IS NULL
THEN
1
WHEN REGEXP_LIKE (
table1.id,
'^( *)(\+|-)?((\d*['
|| SUBSTR (VALUE, 1, 1)
|| ']?\d+)|(\d+['
|| SUBSTR (VALUE, 1, 1)
|| ']?\d*)){1}(e(\+|-)?\d+)?(f|d)?$',
'i')
THEN
1
ELSE
0
END
FROM NLS_SESSION_PARAMETERS
WHERE PARAMETER = 'NLS_NUMERIC_CHARACTERS')Ok.. Use the following code
BEGIN
FOR a IN (SELECT column_name
FROM user_tab_columns
WHERE table_name = 'TABLE1' --and COLUMN_NAME in (...)
LOOP
EXECUTE IMMEDIATE 'UPDATE table1
SET COL1 = ''Y''
WHERE 0 =
(SELECT CASE
WHEN ID IS NULL
THEN
1
WHEN REGEXP_LIKE (
table1.'
|| a.column_name
|| ',
''^( *)(\+|-)?((\d*[''
|| SUBSTR (VALUE, 1, 1)
|| '']?\d+)|(\d+[''
|| SUBSTR (VALUE, 1, 1)
|| '']?\d*)){1}(e(\+|-)?\d+)?(f|d)?$'',
''i'')
THEN
1
ELSE
0
END
FROM NLS_SESSION_PARAMETERS
WHERE PARAMETER = ''NLS_NUMERIC_CHARACTERS'')';
END LOOP;
END;
END;The following select generates the list of column you want to use
SELECT column_name
FROM user_tab_columns
WHERE table_name = 'TABLE1'And in loop they are using in your querry
a.column_name -
Looping thru column names based on a variable value
I have an ArrayCollection with a series of columns named
price1, price2... thru to price10
I'm trying to loop thru these columns based on a variable
value, but can't figure the syntax:
for (var column:int = 1; column < 11; column++){
testPrice = myGrid.selectedItem.price(column); <-- this
doesn't work
}Try testPrice = myGrid.selectedItem["price" + column];
Remember to test that selectedItem isn't null before you try
to assign the values.
TS -
hi All,
please I would like to write a query such that the column names is to be variables.
"select &C1, &C2 from T1 ;" it's working in sql developer but it's not working on apex region.
thanks a lot.Actually, the ADF Faces demo site from Oracle looks (potentially) really cool. I have no idea how it works in the background though so it could be a nightmare. I'd appreciate it if you would post back here once you've given it a try. I'd be genuinely interested to know what it's like to work with.
Its a bit funny because APEX seems to be coming on leaps and bounds but Oracle don't advertise it much (probably because it's free). That could well be a view that is tainted by being an active member of the community though so it would be intriguing to see what people 'from the outside, looking in' feel about it. You got many people there that have opinions about APEX? The java boys at the place I'm contracting at seem to look down on us with disdain!
Cheers
Ben -
Passing a column name as a parameter
Good afternoon,
I was wondering if any of you ever encountered the following situation and found a method to get around it...
I recently created a pivot table using the SQL Query (PL/SQL function body returning SQL query) option.
The table consisted of the following information:
- Date range, Object Type and count.
The final table looks something like this (pardon the lack of fancy markup, hopefully it will get the point across):
Date Object A Object B Object D Object G
01-apr-09 10 16 50 43
02-apr-09 1 15 77 35
03-apr-09 19 0 14 17
04-apr-09 15 7 70 20
05-apr-09 7 6 65 50And it goes on like that.
I had to create the code dynamically because I don't know what Objects I will get in any given date range (for example: I could have something for Object C and nothing for Object D in the month of March). I had a cursor loop through the object types before creating the SQL query that builds the table above.
The table is now working fine. What I'm trying to do now is do a little bit of drilling down in data.
Since everything is dynamically built, I can't give actual column names in my report. Is there any way that, when I click on, for example, the "10" on April 1st in Object A that the column header "Object A" gets passed? #COL02# only passes the value "10" which isn't what I want, and I tried the other variations such as :COL02 and &COL02. without much luck.
If you could shed some light on this problem, that would be greatly appreciated!
Thank you for your time.
IvanHi,
You can achieve passing column name as parameter with the help of jquery.
Below is the sample code
<script src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1.2.6");
</script>
<script type="text/javascript">
//<![CDATA[
$(document).ready( function() {
$('.t20data').each( function(i) {
$td =$(this);
colHeader = $(this).attr("headers");
$td.html('<a href="f?p=' + &APP_ID. + ':3' + ':' + &APP_SESSION. + '::NO:3:P3_COLUMN_HEADER,P3_COLUMN_VALUE:' + colHeader +',' +$td.text()+ '">' + $td.html() + '</a>');
}); //]]>
</script>Note - U need to change t20data , :p3_column_header , :p3_column_value to match you application.
No need to add link individually by going into column attributes
[E X A M P L E |http://apex.oracle.com/pls/otn/f?p=62171:2]
Regards,
Shijesh -
Passing Table Name to Stored Procedure for From Clause
Is it possible to pass a table name to a stored procedure to be used in the From clause? I have the same task to perform with numerous tables and I'd like to use the same SP and just pass the table name in. Something like this:
=======================================
CREATE OR REPLACE PROCEDURE SP_TEST(
in_TABLE IN VARCHAR2,
AS
V_TABLE VARCHAR2(10);
BEGIN
V_TABLE := 'st_' || in_TABLE; -- in_TABLE is 2-3 character string
SELECT some_columns
INTO some_variables
FROM V_TABLE
WHERE some_conditions...;
END;
=======================================
I'm also using the passed table name to assign to variables in the Select and Where clauses. What I'm getting is an error that V_TABLE must be declared. When I hard code the table name, I don't get any errors, even though I'm also using the same method to assign values in the Select and Where clauses.
Thanks,
Ed HollomanYou need to use dynamic SQL whenever you are swapping out object names (tables, columns).
create or replace procedure sp_test
(in_table in varchar2)
is
-- variables
begin
execute immediate 'select a, b, c from st_' || in_table || ' where x = :xval and y = :yval'
into v_a, v_b, v_c using v_x, v_y;
end; -
How to pass column values to narrative view in obiee11g
Hi All,
my report values like this
Month_name value
Jan 100
Feb 500
march 400
April 1000
Now I can display march values in narrative view
Ex:march values is 400.
when i use @1,@2 it will display the Jan values.
And Also i want show the Table view also in same report.Hello,
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
Maybe you are looking for
-
I had an apple ID under my email address and had an icloud plan for an iPhone. We gave the iPhone (with my email address) to my daughter and she has her own iPad as well. I was able to set up her iPad with her own email address, but I cannot figure
-
How is the best way to have Itunes notice your app or drive traffic to the app store?
How is the best way to have Itunes notice your app or drive traffic to the app store? What are the best ways based on experiance that works to drive traffic to your app as a paid game for .99?
-
I'm very pleased so far with my Iphone except for one problem. I always would use my old Ipod in the car every day, and it was easy to use, just plug the aux in to the headphone jack and play it. Switching from song to song if I needed was easy, I ju
-
Sad ipod and not seen in windows and iTunes
I tried to play my ipod mini yesterday and it displayed and exclamation point and a folder. Then I went to the support page and tried the 5 r's. When I reset my ipod teh screens change from the sad face to the folder. I tried to restore, but my ipod
-
Been a member for over five years on and off
I recently decided to start using Skype again after a short while of computer problems and having it removed from my system, now during my down load, it asked me if I wanted a Skype number said it was free on my sign upo page ot the page that shows w