473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

How to get field value when field name is in variable

I have one view with 3 fields like the view name is myview and it has following sql statement.

Expand|Select|Wrap|Line Numbers
  1. select fn,ln,city from tablea
I created a function like below:-

Expand|Select|Wrap|Line Numbers
  1. create function myfun(in t myview, in f varchar(50)) returns varchar(100) as
Now i want to pass row and name of field and let it return the value from function on basis of field name i pass to the function like

Expand|Select|Wrap|Line Numbers
  1. select myfun(*, 'fn') from myview
this suppose to return value of fn from myfun because i'm passing fn as a parameter name.

hope it is clear, if yes, how i can achieve this?
May 14 '10 #1
5 9687
rski
700 Expert 512MB
You can use dynamic SQL. See here for EXECUTE command

http://www.postgresql.org/docs/curre...tatements.html
May 14 '10 #2
No execute command didn't worked for me. this is what i did?

execute 't.' || quote_ident(f) into v;

in this case t and f are two parameters of function (as mentioned in question) and i declare v as a variable to get the value, it is not working

but if i do this

v := t.fn;

i get the value...so not sure how to get the value dynamically.
May 14 '10 #3
rski
700 Expert 512MB
where is select in that query
execute 't.' || quote_ident(f) into v;
Shouldn't there be somehting like that

Expand|Select|Wrap|Line Numbers
  1. execute 'select '||f||' from '|| t into v;
  2.  
Or I do not understand your problem.
May 14 '10 #4
Thanks for quick report but it is not a table where I can use select so it didn't worked. Even if I try

Expand|Select|Wrap|Line Numbers
  1. select fn into v from t 
Without execute, it doesn't work. I think you didn't get my question.
May 14 '10 #5
here is the link to an example and i'm trying to follow this

http://www.postgresql.org/docs/8.1/s...larations.html

Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION concat_selected_fields(in_t sometablename) RETURNS text AS $$
  2. BEGIN
  3.     RETURN in_t.f1 || in_t.f3 || in_t.f5 || in_t.f7;
  4. END;
  5. $$ LANGUAGE plpgsql;'
in my case , field name is dynamic
May 14 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: kaston3 | last post by:
var totalpoints=0; for (counter=1;....){ myvalue=parseFloat(document.forms.elements.value); totalpoints+=myvalue //the fields in elements are always numbers }
1
by: Stu | last post by:
Hi, I am trying to return the data key field value from a dataset when I select a row in a datagrid after the datagrid has been bound to a dataset. When I an the Edit, Update and Cancel row I...
1
by: Wayne Wengert | last post by:
I want to use the contents of one field to be the text the user clicks on for a hyperlink in a datagrid. I am trying the following code but it does not work. What is the correct syntax for this?...
6
by: JohnnyW | last post by:
Hello, I have I simple question that is not clear for me. So, I have a input field (text field) in my html page. User should give input as a string like following 12 345 678,0 If user gives...
2
by: Brett | last post by:
My database has 2 tables: Table1 & Table2. If a field is not null on a record in table2, then the not null fields in table1 that correspond to the records in table1 needs to be updated to match the...
5
by: devx777 | last post by:
Hello, I am trying to find some information or an example on how to build a dynamic query in DB2 that would allow me to join a table which its name is stored as a field value on another table....
2
by: dharmadam_man | last post by:
I want to use MERGE to UPDATE the rows when there is a match. If there is no match, I want to INSERT the rows, and at the same time change a column value to a constant. For example, change the...
7
by: jmar | last post by:
I'm hoping someone out there can give me a little guidance. I have an Access Database "Customer.MDB" with a table "CustInfo" that has the following design: Field: DataType CustID AutoNumber...
1
by: cbayr249 | last post by:
I'm currently making a database for a research project and I keep running into a problem. I have one master form with the tab control and several subforms. Each subform is backed by a table that are...
8
by: xtremebass | last post by:
Hi Bytes.. i have need to assign Mysql Database DataField value to shell script variable . is it possible ? i have tried like this .. got error.. #!bin/sh password=xxx mysql -u root...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.