Search code examples
javasqloracle-sqldeveloper

Insert comma separated values in same field using sql devloper & prepared statement


Below is a prepared statement. I am using the prepared statement to update one column of a table based on a where clause. I'm using java to loop through a bunch of data. After the first update, I am trying to add multiple comma separated values one by one in same column whenever the "where" clause is met. we are using sql developer. Any idea on how to achieve it ? Thanks in advance.

"UPDATE table set column2 = ? WHERE column1 = ?";

sample :

Below is the first update of the table :

column 1 Column 2
Jordan Size7
AirForce Size5

Below is the desired result of multiple subsequent updates to add new values to where clause "Jordan":

column 1 Column 2
Jordan Size7, size9, size12 , size5
AirForce Size5

Solution

  • I would try chage the query to make use of the CONCAT function.

    I don't have the Java code that you are using, but in the for loop you can try doing this

    UPDATE table set table.column2=CONCAT(table.column2, ?) WHERE table.column1=?
    

    In each iteration you will end up with something like

    UPDATE table set table.column2=CONCAT(table.column2, ', size9') WHERE table.column1="Jordan"
    

    You must add the "," in the value you want to be appended on column2.