简体   繁体   English

如何在没有存储过程的情况下获取更新的行主键

[英]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.我从 Oracle 中找到了“RETURNING INTO”子句,但找不到使用 C# 从更新的行中检索所有主键的方法,而无需创建存储过程。 The logic is quite simple, update some rows, get their IDs so multiple threads works on multiple sets of updated rows.逻辑很简单,更新一些行,获取它们的 ID,这样多个线程就可以处理多组更新的行。 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#这将返回一个参考 cursor 您可以在 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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