pradyu pradyu - 2 years ago 68
SQL Question

Issue in inserting values to table through jsp

I got this exception while using one of the answers from stackoverflow.

You can't specify target table 'tablename' for update in FROM clause

This is my query and am using JSP to pass queries:

String queryString = "INSERT INTO tablename(SL_No,candidate,phone,pan,mailid)
VALUES(SELECT (MAX(SL_No)+1 newSL_No from tablename), ?, ?, ?, ? ))";

Thanks in advance.

Answer Source
INSERT INTO tablename(SL_No,candidate,phone,pan,mailid) 
select MAX(SL_No) + 1, ?, ?, ?, ?
from tablename

But actually it looks like you could just use the auto-increment of the SQL engine to do MAX(SL_No) + 1. If you change to column to that then your statement would be

INSERT INTO tablename(candidate,phone,pan,mailid) 
values (?, ?, ?, ?)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download