dbTalk Databases Forums  

Dynamic column name in function

comp.databases.mysql comp.databases.mysql


Discuss Dynamic column name in function in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Jens Riedel
 
Posts: n/a

Default Dynamic column name in function - 09-29-2010 , 02:48 PM






Hello,

I want to use a cursor in a function which has a select with a dynamic
column name and returns the value of this relevant column depending on
the input parameter of the function.

I wrote it like this:

----------------

CREATE FUNCTION `testdb`.`dyncoltest` (my_column_name varchar(255))
RETURNS INT(11)

BEGIN

DECLARE RETURN_VALUE INT default 0;

DECLARE DYNAMIC_SELECT CURSOR for
select my_column_name
from MY_TABLE
limit 1;


open DYNAMIC_SELECT;
FETCH DYNAMIC_SELECT INTO RETURN_VALUE;
close DYNAMIC_SELECT;

RETURN RETURN_VALUE;

END $$

---------------------

If I call this function with "select dyncoltest('age')" it doesn't
return the value of the 'age' column, but the string value 'age' itself.

So the cursor is interpreted like this:

select 'age' from MY_TABLE

instead of

select age from MY_TABLE


How can I achieve that the input parameter is used as column name and
not as string value?

Thx
JEns

Reply With Quote
  #2  
Old   
Mau C
 
Posts: n/a

Default Re: Dynamic column name in function - 09-30-2010 , 02:06 AM






Il 29/09/2010 21.48, Jens Riedel ha scritto:
Quote:
Hello,

I want to use a cursor in a function which has a select with a dynamic
column name and returns the value of this relevant column depending on
the input parameter of the function.
You could try the prepared statements

http://dev.mysql.com/doc/refman/5.0/...tatements.html

Actually I'm not sure that mysql allows you to write prepared statements
inside a function / procedure...

M.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.