Sql – Long Running Stored Procedure from ADO.NET or Classic ADO

adoado.netnetsql servertsql

Assume the Stored Proc takes 10 minutes to run and returns no data.

What is the proper way to call a stored procedure in SQL Server and have your code not wait around for the result? Is there a way to handle it from the T-SQL or the connection? Something that works in ADO.NET and classic ActiveX ADO?

The only way I thought of is:
1) Create a Job in your T-SQL
2) Load your T-SQL based code into Step one of the Job
3) Make sure the last line of your code removes the Job
4) Execute Job (I'm pretty sure this doesn't leave you hanging for a response)
I know this is very bad and hackish… but

Is there some other T-SQL that I'm not thinking of that you can wrap a stored proc in to say "I know there is no response… so I'm choosing not to wait."?

Best Answer

Yes, you can

  1. call it asynchronously, using SqlCommand.BeginExecuteNonQuery()
  2. Or call it asynchronously on a delegate (will use thread from thread pool),
  3. or on a separate thread (created yourself)
  4. In Classic ADO, Use the Option parameter in the Connection's Open method:

    oConnection.Open( , , , adAsyncConnect)