Home » SQL & PL/SQL » SQL & PL/SQL » input argument in function use in select statement
input argument in function use in select statement [message #617341] Fri, 27 June 2014 10:17 Go to next message
Anjas_DA
Messages: 4
Registered: June 2014
Junior Member
Hi community,

i have this code and i think it should work but i always get an bad bind variable exception
create or replace FUNCTION GET_NAMES(TABLENAME IN VARCHAR2) RETURN TERM_ID_TABLE
   PIPELINED AS
   v_idx   NUMBER;
   v_name VARCHAR2(200);
BEGIN
   FOR msg IN (SELECT * from :TABLENAME) 
....


Do i need to bind the input argument first or am i not allowed to do anything like that at all?

I really appreciate any help!

Best regards
Anja
Re: input argument in function use in select statement [message #617342 is a reply to message #617341] Fri, 27 June 2014 10:21 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
you are not allowed to do something like that.

you must abuse EXECUTE IMMEDIATE to operate against "dynamic" SQL
Re: input argument in function use in select statement [message #617343 is a reply to message #617341] Fri, 27 June 2014 10:31 Go to previous messageGo to next message
flyboy
Messages: 1903
Registered: November 2006
Senior Member
Hi,

You cannot bind identifiers (table and column names) - their names have to be hardcoded.
http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:227413938857
So, you have to use dynamic SQL statement for this requirement, which can be handled in explicit cursor processing using OPEN FOR ... FETCH ... CLOSE statements only.

As you use star (*) in SELECT clause, the next challenge will be "dynamic" extraction of column names inside the cursor.
Which leads to the question: which action "generic" enough are you planning to do there?
Re: input argument in function use in select statement [message #617344 is a reply to message #617341] Fri, 27 June 2014 10:34 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
In your case, lets say using SQL*Plus, bind variable only exists in the client. Once you exist, Its lost. Server side won't be able to know about it as object resides in DB not locally. Execute immediate is one option.

Else, and quite obvious reason as I explained about client side, you can have it in a local variable and give it a try.

And select * inside a code is just...whatever...you should know what results you want, isn't it?

[Updated on: Fri, 27 June 2014 10:37]

Report message to a moderator

Re: input argument in function use in select statement [message #617346 is a reply to message #617344] Fri, 27 June 2014 10:50 Go to previous messageGo to next message
Anjas_DA
Messages: 4
Registered: June 2014
Junior Member
Thanks a lot for the answers!

So I will try the Execute Immediate Way. I can also use fixed column names, because the tables I want to query all look the same.
Just to be sure: I am allowed to use EXECUTE IMMEDIATE in a pipelined function?

You guys are great!
Re: input argument in function use in select statement [message #617349 is a reply to message #617346] Fri, 27 June 2014 10:54 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Answers in forums might just be a workaround and not preferably suitable in your environment/application. Beware of dynamic sql, you don't want to expose your table names to someone, isn't it?
Re: input argument in function use in select statement [message #617351 is a reply to message #617349] Fri, 27 June 2014 10:57 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>I can also use fixed column names, because the tables I want to query all look the same.
Why does this make me think that the "designer" did not Normalize the data.

Re: input argument in function use in select statement [message #617353 is a reply to message #617349] Fri, 27 June 2014 11:08 Go to previous messageGo to next message
Anjas_DA
Messages: 4
Registered: June 2014
Junior Member
In my case I need a generic tablename, because I call the function from inside python. I also thought about coding for each table i am querying a special function (from inside python). But i think that is really ugly, but it would be possible. Are there any threats i have to deal with using dynamic sql?
Re: input argument in function use in select statement [message #617355 is a reply to message #617351] Fri, 27 June 2014 11:18 Go to previous messageGo to next message
Anjas_DA
Messages: 4
Registered: June 2014
Junior Member
The different tables with the same structure are filled from another programm. I want to differentiate the data from each instance of this program. I could have done it also in one table, but the tables get really large with time.

[Updated on: Fri, 27 June 2014 11:20]

Report message to a moderator

Re: input argument in function use in select statement [message #617366 is a reply to message #617355] Fri, 27 June 2014 12:11 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>I could have done it also in one table, but the tables get really large with time.
The combined total size of the small tables will be essentially the same as a single combined table.
The single table would be MUCH, MUCH, much easier to code.
You reap what you sow.
Re: input argument in function use in select statement [message #617368 is a reply to message #617366] Fri, 27 June 2014 12:32 Go to previous message
flyboy
Messages: 1903
Registered: November 2006
Senior Member
@BlackSwan: I would not be so strict if the purpose of this design was avoiding the use of OTLT (one true lookup table)
http://tonyandrews.blogspot.cz/2004/10/otlt-and-eav-two-big-design-mistakes.html
https://www.simple-talk.com/sql/t-sql-programming/look-up-tables-in-sql-/
(just for future readers: it would be really very bad idea to follow these examples)

Anyway, back to dynamic SQL. There are many documents about Oracle database features. A good first choice would be studying PL/SQL User's Guide and Reference, which is available e.g. online on http://docs.oracle.com/
You may find many other pages demonstrating usage of (dynamic) cursors, e.g. this one:
http://www.oracle-base.com/articles/8i/native-dynamic-sql.php

Good luck.
Previous Topic: find all the null data value in column
Next Topic: Sort data output to excel (owa_util) from 2 cursor
Goto Forum:
  


Current Time: Thu Apr 25 17:05:49 CDT 2024