I thought the update statement required data to already exist.

I am supporting a java app. The previous developer for some code used a delete statement to clear out the record then used an update statement to insert data back into the table.

Can the update statement be used to insert or update data into a table???

The db is oracle express. that should not make a difference.

有帮助吗?

解决方案

You can try using MERGE statement for getting the same result. With MERGE statement you can either insert new records into table or update if the data already exists.

Please find the syntax of the MERGE statement.

MERGE INTO <table_name>
USING <table_view_or_query>
ON (<condition>)
WHEN MATCHED THEN <update_clause>
DELETE <where_clause>
WHEN NOT MATCHED THEN <insert_clause>;

Perhaps you can give more details about you problem. I am assuming this is what is required by you.

其他提示

Query is an instance of org.hibernate.Query. Looking at the hibernate documentation for query. it the update should thrown an exception, but it is not. could the fact that the code is not flushing the buffer be the reason why the update is not throwing an exception???

String deleteQueryString = String.format("delete FROM %s WHERE personId = %s and deptId = %s and active = true", table, person.getId(),dept.getId());

Query deleteQuery = Persistence.reportContext().session().createQuery(deleteQueryString);
int deletedCount = deleteQuery.executeUpdate();
log.debug(String.format("%20s (%4s): %s", "delete query", deletedCount, deleteQueryString));

String updateQueryString = String.format("update %s set activeSet = true WHERE personId = %s and deptId = %s and activeSet = false", table, person.getId(), dept.getId());

Query updateQuery = Persistence.reportContext().session().createQuery(updateQueryString);
int updatedCount = updateQuery.executeUpdate();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top