Java/Oracle Bind Variables - Help! [message #147477] |
Thu, 17 November 2005 16:10 |
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 #147607 is a reply to message #147480] |
Fri, 18 November 2005 09:03 |
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>
|
|
|
|
|