sql >> Base de Datos >  >> RDS >> Oracle

ACTUALIZACIÓN DE ODP.NET... REGRESANDO A... varias filas, tipo de parámetro

finalmente, después de horas de buscar y jugar con el código, llegué a las siguientes conclusiones (aparte del dolor de cabeza):

Obtuve lo que quería usando la combinación de

  1. una pista aquí , que sugirió envolver la instrucción UPDATE..RETURNING en un bloque PL/SQL anónimo (comenzar con BEGIN y terminar con END;) - esto fue sin explicación y todavía no sé exactamente por qué el comportamiento es diferente
  2. fragmento de código en la documentación de Oracle sobre OracleCommand, específicamente la parte sobre la vinculación de PL /Arreglos asociativos SQL con BULK COLLECT INTO (no se pudo hacer funcionar el enlace de matriz simple...):

try
{
    conn.Open();
    transaction = conn.BeginTransaction();

    cmd = new OracleCommand();
    cmd.Connection = GetConnection();

    cmd.CommandText =
        "BEGIN UPDATE some_table " +
        "SET status = 'locked', " +
        "    locked_tstamp = SYSDATE, " +
        "    user_name = '" + user + "' " +
        "WHERE rownum <= 4 " +
        "RETURNING id BULK COLLECT INTO :id; END;";

    cmd.CommandType = CommandType.Text;

    cmd.BindByName = true;
    cmd.ArrayBindCount = 4;

    p = new OracleParameter();
    p.ParameterName = "id";
    p.Direction = ParameterDirection.Output;
    p.OracleDbType = OracleDbType.Int64;
    p.Size = 4;
    p.ArrayBindSize = new int[] { 10, 10, 10, 10 };
    p.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
    cmd.Parameters.Add(p);

    int nRowsAffected = cmd.ExecuteNonQuery();

    // nRowsAffected is always -1 here
    // we can check the number of "locked" rows only by counting elements in p.Value (which is returned as OracleDecimal[] here)
    // note that the code also works if less than 4 rows are updated, with the exception of 0 rows
    // in which case an exception is thrown - see below
    ...
}
catch (Exception ex)
{
    if (ex is OracleException && !String.IsNullOrEmpty(ex.Message) && ex.Message.Contains("ORA-22054")) // precision underflow (wth)..
    {
        Logger.Log.Info("0 rows fetched");
        transaction.Rollback();
    }
    else
    {
        Logger.Log.Error("Something went wrong during Get : " + ex.Message);
        ret = null;
        transaction.Rollback();
    }
}
finally
{
    // do disposals here
}
...