Home » Developer & Programmer » JDeveloper, Java & XML » Java/Oracle Bind Variables - Help!
Java/Oracle Bind Variables - Help! [message #147477] Thu, 17 November 2005 16:10 Go to next message
svguerin3
Messages: 44
Registered: November 2005
Location: TX
Member

SELECT field1
FROM ?
WHERE field2 IN (?)

There's my query, I want a bind variable for the tablename and a bind variable for the IN-clause. Both don't work! When you use the query.setString() method, it automatically attaches quotes and converts the strings to VARCHAR, so the query never works (the IN-clause bind variable only works when I have 1 value...not very useful).

This seems like a simple problem, but it has me stumped! Any ideas on how to use bind variables with the tablename and the IN-clause? Thanks!
-Vince
Re: Java/Oracle Bind Variables - Help! [message #147480 is a reply to message #147477] Thu, 17 November 2005 16:33 Go to previous messageGo to next message
svguerin3
Messages: 44
Registered: November 2005
Location: TX
Member

I should also mention that the point to what I'm doing is to avoid using any literals, so building something like dynamic SQL instead of using the bind-variables is out of the question. Smile

Thanks
Re: Java/Oracle Bind Variables - Help! [message #147607 is a reply to message #147480] Fri, 18 November 2005 09:03 Go to previous messageGo to next message
Art Metzer
Messages: 2480
Registered: December 2002
Senior Member
From here:
SQL> CREATE OR REPLACE TYPE mytabletype AS TABLE OF VARCHAR2 (255)
  2  /
 
Type created.
 
SQL> CREATE OR REPLACE FUNCTION in_list (
  2      p_string IN VARCHAR2
  3  )
  4  RETURN mytabletype
  5  AS
  6      l_string        LONG DEFAULT p_string || ',';
  7      l_data          mytabletype := mytabletype();
  8      n               NUMBER;
  9  BEGIN
 10      LOOP
 11          EXIT WHEN l_string IS NULL;
 12          n := INSTR(l_string, ',');
 13          l_data.EXTEND;
 14          l_data(l_data.COUNT) := TRIM(SUBSTR(l_string, 1, n-1));
 15          l_string := SUBSTR(l_string, n + 1);
 16      END LOOP;
 17
 18      RETURN (l_data);
 19  END in_list;
 20  /
 
Function created.
 
SQL> SELECT empno
  2  ,      ename
  3  FROM   emp
  4  WHERE  ename IN (SELECT * FROM TABLE(in_list('ADAMS,BLAKE,JONES')))
  5  /
 
     EMPNO ENAME
---------- ----------
      7876 ADAMS
      7698 BLAKE
      7566 JONES
 
SQL>
Re: Java/Oracle Bind Variables - Help! [message #147627 is a reply to message #147607] Fri, 18 November 2005 11:03 Go to previous messageGo to next message
svguerin3
Messages: 44
Registered: November 2005
Location: TX
Member

Yea, I've tossed around the idea of calling an pl/sql proc to handle this, but I was really trying to do it within the java code if possible. Thanks for the quick reply, though.

I found a way to rig the IN-clause to work for a variable amount of fields, but I'm still stumped on whether it's possible to have table-names set as bind variables within java... Any ideas?
Re: Java/Oracle Bind Variables - Help! [message #147645 is a reply to message #147627] Fri, 18 November 2005 14:18 Go to previous message
Art Metzer
Messages: 2480
Registered: December 2002
Senior Member
You can't bind table names or other indentifiers into a query--it doesn't really make sense to. For more information, please read this AskTom thread.
Previous Topic: Upload word doc into Oracle BLOB
Next Topic: xml query problem
Goto Forum:
  


Current Time: Fri Apr 26 07:47:37 CDT 2024