【发布时间】:2013-01-24 23:04:58
【问题描述】:
下面是我的表,其中ID column is Primary Key。其他两列是字符串。
我正在使用一个名为 XpressMP 的新数据库。
Column Name
-------
ID PrimaryKey
SEARCHES String
ACCOUNT String
我正在尝试在此处实现UPSERT 的功能-
If ID doesn't exists here then insert a new record.
And ID exists then update the record.
我知道如果我正在使用 Oracle,那么我可以使用 MERGE sql 命令,但该数据库不支持 MERGE,并且目前没有其他命令。但我相信我可以用Stored Procedure 做同样的事情。
谁能提供一些建议,我如何使用存储过程做同样的事情?因为存储过程将在那里工作。
更新:-
public final static String INSERT = "BEGIN"
+" INSERT INTO TABLE (ID, SEARCHES, ACCOUNT) VALUES (?, ?, ?)"
+" EXCEPTION"
+" WHEN DUP_VAL_ON_INDEX THEN"
+" UPDATE TABLE"
+" SET SEARCHES = ?, ACCOUNT = ?"
+" WHERE ID = ?"
+" END";
每当我尝试像这样执行上述存储过程时
preparedStatement = dbConnection.prepareStatement(INSERT);
preparedStatement.setString(1, String.valueOf(userId));
preparedStatement.setString(2, Constants.getaAccount(userId));
preparedStatement.setString(3, Constants.getaAdvertising(userId));
preparedStatement.executeUpdate();
我得到异常?这是执行它的正确方法吗?
【问题讨论】:
标签: sql database stored-procedures plsql