简体   繁体   中英

How to get the updated rows primary key without stored procedure

I have a situation where I need to retrieve the primary key of the updated rows in a simple update statement. I found the "RETURNING INTO" clause from Oracle, but couldn't find a way to retrieve all the primary keys from the rows that were updated, using C#, without create a stored procedure. The logic is quite simple, update some rows, get their IDs so multiple threads works on multiple sets of updated rows. Is it possible to achieve?

You do not need to create a stored procedure but it won't work with a simple update statement. you need at least something like this as statement:

DECLARE t_ids id_array;
BEGIN
UPDATE table
SET field1 = :value
WHERE field2 = :searchFor
RETURNING key_column BULK COLLECT INTO t_ids;

OPEN :rcursor FOR SELECT * FROM TABLE(cast(t_ids as id_array));
END;

This will return a ref cursor which you could process in c#

OracleCommand command = new OracleCommand("<the sql query above>", connection);
command.BindByName = true;

// Add the parameters for update statement
// ...

// Add the out parameter for the ref cursor
OracleParameter refCursor = new OracleParameter("rcursor", OracleDbType.RefCursor);
refCursor.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returnCursor);

var reader = command.ExecuteReader();

OracleDataAdapter dataAdapter = new OracleDataAdapter(command);
DataTable dt = new DataTable();
dataAdapter.Fill(dt);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM