Home » SQL & PL/SQL » SQL & PL/SQL » How to write stored function to return column name?
How to write stored function to return column name? [message #630292] Wed, 24 December 2014 03:55 Go to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Hi all,

How to write stored function to return column name of table let say date and need return type is varchar2? and display that column all the values

Thanks

[Updated on: Wed, 24 December 2014 03:58]

Report message to a moderator

Re: How to write stored function to return column name? [message #630296 is a reply to message #630292] Wed, 24 December 2014 04:28 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

What did you try so far?

Re: How to write stored function to return column name? [message #630297 is a reply to message #630296] Wed, 24 December 2014 04:32 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:
What did you try so far?

Thanks for reply michel.. Smile
CREATE OR REPLACE FUNCTION xxc_coluimn
   RETURN VARCHAR2
AS
   v_date   VARCHAR2 (10);
BEGIN
   SELECT TO_CHAR (COLUMN_NAME)
     INTO v_date
     FROM user_tab_columns
    WHERE table_name = 'EMP' 
    AND column_name = 'HIREDATE';
   RETURN v_date;
END;

The above code will return hiredate column and return type is varchar so,
DECLARE
   v_column   VARCHAR2 (30);
   v_sql      VARCHAR2 (1000);
BEGIN
   SELECT xxc_coluimn (i.HIREDATE)
     INTO v_column
     FROM emp i;
v_sql := 'SELECT '||v_column||' FROM emp '
execute immediate v_sql;
dbms_output.put_line ('column value'||v_sql);
end;

Suppose if i use like this i must to use return into for execute immediate

I am trying to do is need to return that column all the data using dynamic query
Re: How to write stored function to return column name? [message #630300 is a reply to message #630297] Wed, 24 December 2014 04:49 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
   SELECT TO_CHAR (COLUMN_NAME)
     INTO v_date
     FROM user_tab_columns
    WHERE table_name = 'EMP' 
    AND column_name = 'HIREDATE';

1/ If you know the column name why do you query USER_TAB_COLUMNS to get the column name? The result is quite obvious
2/ COLUMN_NAME is of VARCHAR2 data type so isn't "TO_CHAR (COLUMN_NAME)" quite silly?

Quote:
SELECT xxc_coluimn (i.HIREDATE)
     INTO v_column
     FROM emp i;

What is the meaning of this?

Quote:
I am trying to do is need to return that column all the data using dynamic query


Loop on a cursor.

You have to work a little bit harder on this and first think about what you write.
What is the input? What is the output? It seems you want all the values of... of what?

[Updated on: Wed, 24 December 2014 04:50]

Report message to a moderator

Re: How to write stored function to return column name? [message #630303 is a reply to message #630300] Wed, 24 December 2014 04:59 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:
What is the input? What is the output? It seems you want all the values of... of what?

No inputs are there and i need to get all values of hiredate column
Quote:


Quote:
I am trying to do is need to return that column all the data using dynamic query


Loop on a cursor.

You said Loop a cursor so where i can i use Loop,i mean in which query
Re: How to write stored function to return column name? [message #630304 is a reply to message #630296] Wed, 24 December 2014 05:01 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
example select function_return_varchar2( i.date) from items i ;
Hint:function_return_... will return column name in table items
and i wanna retrieve all data in this column
Re: How to write stored function to return column name? [message #630306 is a reply to message #630303] Wed, 24 December 2014 05:14 Go to previous message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
No inputs are there and i need to get all values of hiredate column


select HIREDATE from EMP;

Previous Topic: source does not have runnable target
Next Topic: Complicate Query
Goto Forum:
  


Current Time: Mon Aug 24 21:28:18 CDT 2026