I'm trying to execute a SQL command that insert a record on a table and returns the generate primary key.
I'm using .NET Core 3.1 and Oracle.ManagedDataAccess.Core package.
This is the C# code to execute the SQL command (it uses some extension methods but is clear how it works):
private int PutSomethingInTheDatabase(string entity)
{
string sqlComamnd = File.ReadAllText("SQL//Insert Card.sql");
using (var connection = new Oracle.ManagedDataAccess.Client.OracleConnection(connectionString))
using (var command = connection.OpenAndUse().CreateTextCommand(sqlComamnd))
{
//var reader = command.ExecuteReader();
//reader.Close();
//var result = command.ExecuteScalar();
//return (int)(decimal)result;
return -1;
}
}
Ideally I will receive a single value and read it with ExecuteScalar().
It is an itegration test (thats why I read the SQL from a file).
The SQL I want to use should INSERT the new record and return the generated sequence within the same scope/transaction, that's whi I'm using Begin/End but I'm not sure it is the right way.
My problem is that I cannot find the right syntax to execute the last SELECT to return the generated sequence_id, I also tried with RETURN...
This is the SQL:
declare new_id number;
BEGIN
select seq_stage_card.NEXTVAL into new_id from dual;
INSERT INTO spin_d.stage_card (
sequence_id,
field_1,
field_2
)
VALUES (
new_id,
'aaa'
TO_DATE('2003/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss')
);
select new_id from dual where 1 = 1 ; -- not valid
END;
-- return new_id ; -- not valid
-- select new_id from dual ; -- not valid
How to change the SQL in order to return the new_id ? There is another (better) way to achieve the same result? Is it safe (isolated scope), or the select will return a wrong ID if there is a concurrent insert?
[Update]
Someone suggested to use RETURNING (see here: Oracle - return newly inserted key value)
I already tried to use RETURN and RETURNING but I haven't find any real example of usage with the .NET (or other frameworks) driver, eg. OracleSqlCommand and the right call to execute.
Maybe it works but I still cannot figure out how to use it.