I am developping an asp.net application. I would like to know how to update a field of an oracle table record, and if the record is not present, inserting it.
I have a table with the following fields NAME and SURNAME.
I would like to change the SURNAME to "new_surname" of the record where the NAME equals="name". However, if none of the records in table contains a field NAME equals to name I would like to insert a new record (SURNAME=new_surname and NAME=name).
This is my code :
OracleConnection connection = new OracleConnection(connectionstring);
try
{
connection.Open();
OracleCommand command = connection.CreateCommand();
string sql = "UPDATE TABLE SET SURNAME=\'new_surname\' WHERE NAME=\'name\'";
command.CommandText = sql;
command.ExecuteNonQuery();
connection.Close();
}
catch (Exception exp)
{
}
Is there an optimal way to do the insert only if the update find zero records matching the "where" clause. I was thinking of first doing a select count of the record matching the "where" clause, and then if I found zero results I would do an insert, and if I found at least one result I would do an update. But I find this solution a little bit heavy.