Using a preparedStatement in Java (under DB2) we want to control the LAST access time of each user to the app.
So, we have a table named 'users_access' with two fields: 'user_id' and 'access_date'.
Does somebody know if it is possible to use SQL 'merge into' sentence to insert the record if it is the first time the user connects; or update the record (in concrete the date) in successive loggings.
This works only if the record yet exists:
MERGE INTO users_access a USING
(SELECT user_id FROM users_access WHERE user_id = ?) b
ON (a.user_id = b.user_id)
WHEN MATCHED THEN
UPDATE SET access_date = ?
WHEN NOT MATCHED THEN
INSERT (
user_id, access_date
) VALUES (
?, ?
)