Reputation: 205
I have two select statements and make 'union all' for these two statements. Then, I use the PreparedStatement and when I setString to this preparedStatement, it shows "java.sql.SQLException: Missing IN or OUT parameter at index:: 2".
After I toured around google, some people say that for each "?" in sql statment, I should write setString. For my situation, I have two select statments so I have two "?" but I "union all", so I'm not sure whether it is assumed that one "?" or two "?". But when I tried to write two setString like preparedStatement.setString(1,ApplicationNo); preparedStatement.setString(2,ApplicationNo); , it shows "ORA-00918: column ambiguously defined".
I have no idea how to solve this problem.
my union select statment is
query.append("select TO_CHAR(TRUNC(SYSDATE),'DD MONTH,YYYY'),a.appl_no,a.assigned_to,b.co_name,b.co_name2,a.credit_acct_no,a.credit_bank_no,a.credit_branch_no,a.service_id ");
query.append("from newappl a, newappl_hq b where b.appl_no = a.appl_no and a.appl_no=(select appl_no from newappl where appl_no=?) and rownum=1 and credit_status = 'CRPEND'");
query.append(" union all ");
query.append("select TO_CHAR(TRUNC(SYSDATE),'DD MONTH,YYYY'),a.appl_no,a.assigned_to,c.trading_name co_name, ' ' co_name2, d.bank_acct_no credit_acct_no, d.bank_no credit_bank_no, d.bank_branch_no credit_branch_no,a.service_id ");
query.append("from newappl a,newappl_hq b, newappl_ret c, newappl_ret_bank d where b.appl_no = a.appl_no or a.appl_no = c.appl_no and c.ret_id= d.ret_id and a.appl_no=(select appl_no from newappl_ret where appl_no=?) and rownum=1 and credit_status = 'CRPEND'");*
setString is preparedStatement.setString(1,ApplicationNo);
When I searched for setString example, there are two different parameters if there are two setString like
preparedStatement.setString(1,ApplicationNo);
preparedStatement.setString(2,LoginID);
But I need ApplicationNo for both select statments.
Upvotes: 0
Views: 1533
Reputation: 1
I also had same exception while running query in sql developer. below solution worked for me, you can also have a try. You should rewrite the query, try and see if you can use ambiguous column table in FROM clause instead of putting it inside of JOIN expression.
Upvotes: 0
Reputation: 332561
I re-wrote your query as:
SELECT TO_CHAR(SYSDATE,'DD MONTH,YYYY'),
a.appl_no,
a.assigned_to,
b.co_name,
b.co_name2,
a.credit_acct_no,
a.credit_bank_no,
a.credit_branch_no,
a.service_id
FROM newappl a
JOIN newappl_hq b ON b.appl_no = a.appl_no
WHERE a.appl_no = ?
AND rownum = 1
AND credit_status = 'CRPEND'
UNION ALL
SELECT TO_CHAR(SYSDATE,'DD MONTH,YYYY'),
a.appl_no,
a.assigned_to,
c.trading_name,
' ',
d.bank_acct_no,
d.bank_no,
d.bank_branch_no,
a.service_id
FROM newappl a
JOIN newappl_ret c ON c.appl_no = a.appl_no
JOIN newappl_ret_bank d ON d.ret_id = c.ret_id
WHERE c.appl_no = ?
AND rownum = 1
AND credit_status = 'CRPEND'
From what I can see, the ORA-00918 is about the reference to the credit_status
column. Of the tables involved, is there a credit_status
column in more than one of them? Because it's the only un-aliased column in either query.
Couple other things to mention:
Upvotes: 4
Reputation: 308753
I see no reason why you should be building this query up and gc-ing it away over and over. I'd make it a static final String once and be done with it.
If you need it twice, why can't you do this?
ps.setString(1, applicationNumber);
ps.setString(2, applicationNumber);
Upvotes: 1