Question

I am learning Java and have big problem with SQL query. I have field that should be updated if exist or inserted if not exist.

Firstly I tried with IF NOT EXISTS, but it turns out that Oracle doesn't support that.
Then I tried with EXCEPTION WHEN NO_DATA_FOUND, but it also failed.
After few hours of searching I found out that MERGE must work on my example, but for some reason I couldn't make it to work. I have never used merge before, so maybe I made mistake in query, but I don’t think that’s the problem.

So, I have table commercial and field in it idArt, idPla, quan and ID (unique and auto increment). From html form I am sending first three values to servlet and then servlet should do next thing:

  • if there is row with idArt=(formIdArt) AND idPla=(formIdPla) then update quan=quan+(formQuan), ELSE insert all three values.

This looks simple, but I have already lost way too much time on this, so can please someone help me with this?

Was it helpful?

Solution

First,

Try to update your table like this:

update commercial set quan = quan + formquan where idArt=formIdArt and idPla = formIdPla

if execution of this query returns result as more than 0 row affected then do not insert all three values, else insert all three values in your table

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top