UPSERT Statements
An UPSERT statement updates an existing record or creates a new record if an existing record is not identified.
UPSERT Syntax
The UPSERT syntax is the same as for insert. NetSuite uses the input provided in the VALUES clause to determine whether the record already exists. If the record does not exist, all columns required to insert the record must be specified. See Data Model for any table-specific information.
UPSERT INTO <table_name>
( <column_reference> [ , ... ] )
VALUES
( { <expression> | NULL } [ , ... ] )
<expression> ::=
| @ <parameter>
| ?
| <literal>
You can use the executeUpdate method of the Statement and PreparedStatement classes to issue data manipulation commands and retrieve the rows affected, as shown in the following example:
To retrieve the InternalId of the last inserted record, use getGeneratedKeys. Additionally, set the RETURN_GENERATED_KEYS flag of the Statement class when you call prepareStatement.
String cmd = "UPSERT INTO Account (AcctName) VALUES (?)";
PreparedStatement pstmt = connection.prepareStatement(cmd,Statement.RETURN_GENERATED_KEYS);
pstmt.setString(1, "Petty Cash");
int count = pstmt.executeUpdate();
System.out.println(count+" rows were affected");
ResultSet rs = pstmt.getGeneratedKeys();
while(rs.next()){
System.out.println(rs.getString("InternalId"));
}
connection.close();