SQL Server 2008 – Is Order Guaranteed When Calling Two Stored Procedures?

sql-server-2008stored-procedures

I have some code that looks like this:

    EXEC StoredProcedure1 @Arguments

    EXEC StoredProcedure2 @Arguments

Are the two datasets guaranteed to return in the called order? I am expecting two result sets to be passed back to my server code and am running into some issues and I feel the ordering may not be guaranteed. If it is not guaranteed, is there a way to enforce ordering?

Not the ordering of the data, but ordering of StoredProcedure1 always being returned as result set 1, and StoredProcedure2 always being returned as result set 2.

Best Answer

Yes. StoredProcedure1 will execute first, and any resultsets from StoredProcedure1 must be consumed by the client before StoredProcedure2 is executed.

It's possible that some client driver or DAL processes all the resultsets and then returns them to your calling code in a different order, but for SQL Server and the Microsoft client drivers, you will get the resultsets in the order in which they are produced.