I'm using Delphi XE2 to call a stored procedure that returns data. The stored procedure is basically:
set nocount on -- ensures that Delphi will see that dataset
...
exec StoredProcThatReturnsData
...
update [table]
update [another table]
When the main procedure is a T-SQL procedure, it finishes executing the main procedure including the last update
statements before it returns the data to Delphi.
When the main procedure is a CLR procedure, it returns the data at the moment the sub-procedure finishes executing and doesn't run the update
statements until I close the TSQLQuery
object in Delphi.
The Delphi code looks like this:
rpt := TSQLQuery.Create(nil);
try
rpt.MaxBlobSize := -1;
rpt.SQLConnection := ASqlConnection;
rpt.SQL.Text := 'exec RunMainProc';
rpt.Open;
// do some things that depend on the update statements
// process the 'rpt' data
finally
FreeAndNil(rpt);
end;
The CLR proc looks like this:
[Microsoft.SqlServer.Server.SqlProcedure]
public static void RunReport(SqlGuid ReportID, SqlGuid UserID, SqlBoolean ForDownload)
{
using (SqlConnection con = new SqlConnection("context connection=true"))
{
con.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandText = "[identical SQL as T-SQL proc]";
SqlContext.Pipe.ExecuteAndSend(cmd);
}
}
}
Why does the T-SQL procedure finish executing before returning the data while the CLR procedure waits to run the update
statements until after Delphi closes/destroys the rpt
query?