Oracle
 SQL >> Base de données >  >> RDS >> Oracle

Corriger « Fetch Out of Sequence » en renvoyant une table depuis une fonction Oracle via un DB Link

Après plus d'une journée à creuser, je suis tombé sur la réponse dix minutes à peine après avoir posté la question. C'est classique !

La solution est ici - https://community.oracle.com/thread/659625 - et il suffit d'envelopper le code appelant dans une transaction. Le code qui fonctionne ressemble à ceci :

using (var connection = new OracleConnection(connstring))
{
    connection.Open();
    using (var command = connection.CreateCommand())
    {
        // Démarre une transaction locale
        using (var transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted))
        {
            // Assigne l'objet transaction à la transaction locale en attente
            command.Transaction = transaction;
            command.CommandText = "FNC_AXA_APPTS";
            command.CommandType = CommandType.StoredProcedure;
            OracleParameter retVal = new OracleParameter("PRS", OracleDbType.RefCursor);
            retVal.Direction = ParameterDirection.ReturnValue;
            command.Parameters.Add(retVal);
            command.Parameters.Add(new OracleParameter("EG_PARAM", OracleDbType.Varchar2, 50)).Value = paramValue;
            command.ExecuteNonQuery();
            using (OracleDataReader reader = ((OracleRefCursor)command.Parameters["PRS"].Value).GetDataReader())
            {
                dt.Load(reader);
            }
        }
    }
}

D'après ma compréhension limitée, sans transaction explicite, une transaction est validée côté SQL Server, ce qui fait échouer le curseur renvoyé lors de son itération une fois transmis au code .NET. Si quelqu'un a une meilleure explication, qu'il l'ajoute à la question.