I have an SQL file that looks like this:
DECLARE
V_ID_CB VARCHAR2(15) := '{V_ID_CB}';
V_X_CB VARCHAR2(15) := '{V_X_CB}';
BEGIN
IF V_ID_CB IS NOT NULL THEN
OPEN :result_cursor FOR
SELECT * FROM DEMANDES WHERE ID_CB = V_ID_CB;
ELSIF V_X_CB IS NOT NULL THEN
OPEN :result_cursor FOR
SELECT * FROM SITES;
END IF;
END;
I read this file in C# and then use the Replace command to replace the value of the variable with the actual value:
var content = System.IO.File.ReadAllText(filePath);
var sqlSkript = content;
foreach (var param in parameters)
{
sqlSkript = sqlSkript.Replace($"{{V_{param.Key}}}", param.Value);
}
public DataTable Execute(string sqlSkript)
{
try
{
if (!(_oraController.OracleConnection.State == ConnectionState.Open))
{
_oraController.OracleConnection.Open();
}
using (var command = new OracleCommand(sqlSkript) { Connection = _oraController.OracleConnection })
{
command.CommandType = CommandType.Text;
OracleParameter resultCursor = new OracleParameter
{
ParameterName = "result_cursor",
OracleDbType = OracleDbType.RefCursor,
Direction = ParameterDirection.Output
};
command.Parameters.Add(resultCursor);
using (var reader = command.ExecuteReader())
{
DataTable dataTable = new DataTable();
dataTable.Load(reader);
return dataTable;
}
}
}
catch (Exception ex)
{
string errorMessage = ex.Message + "\n" + ex.StackTrace;
throw new Exception(errorMessage, ex);
}
}
This also works so far and I get the result of the SELECT-Statement in a DataTable.
Now I would like to query several tables as output parameters. How is this possible? Is that even possible? I only want to work with a single CURSOR.
I have tried to achieve my goal by using several CURSORs. However, this SQL file will constantly grow, which would mean that I would have to define a new “result_cursor” in my code for each new SELECT. This is not a solution for me.