Use SQL jobs in Asp.net to Implement Asynchronous calling of stored procedures.

Source: Internet
Author: User


Because of Asp.net's BS mechanism, we have been executing a complex, ultra-long stored procedure for a long time, the page times out and stops responding. This method can be implemented through SQL jobs.ProgramTemporary Job for asynchronous call

/*************************************** ***************************************
* Create a temporary job in SQL Server 2000 for executing asynchronous calls.
* @ Execsql: Transact-SQL batch
* Eample: exec DBO. asynchronousinvoking @ execsql = 'updtae customer set balance = 0'
* Disadvantage: the database name must be specified for this stored procedure.
**************************************** **************************************/
Create procedure Bak
@ Execsql nvarchar (4000 ),
@ Job nvarchar (50 ),
@ Servern nvarchar (50 ),
@ Dbname nvarchar (50)
As

Begin transaction
Declare @ jobid binary (16)
Declare @ returncode int
Select @ returncode = 0

Begin

-- Add the job
Execute @ returncode = MSDB. DBO. sp_add_job @ job_id = @ jobid output,
@ Job_name = @ job,
@ Owner_login_name = n '',
@ Description = n 'description for job', -- the description of the job
@ Category_name = n' [uncategorized (local)] ',
@ Enabled = 1,
@ Policy_level_email = 0,
@ Policy_level_page = 0,
@ Policy_level_netsend = 0,
@ Policy_level_eventlog = 0,
@ Delete_level = 3

If (@ error <> 0 or @ returncode <> 0) goto quitwithrollback

-- Add the job steps
Execute @ returncode = MSDB. DBO. sp_add_jobstep @ job_id = @ jobid,
@ Step_id = 1,
@ Step_name = n'step1 ',
@ Command = @ execsql, -- SQL batch
-- Disadvantage: the database name must be specified for the stored procedure.
@ Database_name = @ dbname, -- the Database Name of the job to manipulate
@ Server = n '',
@ Database_user_name = n '',
@ Subsystem = n'tsql ',
@ Cmdexec_success_code = 0,
@ Flags = 0,
@ Retry_attempts = 0, -- execute once only
@ Retry_interval = 0,
@ Output_file_name = n '',
@ On_success_step_id = 0,
@ On_success_action = 1, -- On success abort
@ On_fail_step_id = 0,
@ On_fail_action = 2 -- On Fail abort

If (@ error <> 0 or @ returncode <> 0) goto quitwithrollback

-- Set the star step ID of the job
Execute @ returncode = MSDB. DBO. sp_update_job @ job_id = @ jobid,
@ Start_step_id = 1

If (@ error <> 0 or @ returncode <> 0) goto quitwithrollback

-- Add the target servers
Execute @ returncode = MSDB. DBO. sp_add_jobserver @ job_id = @ jobid,
@ SERVER_NAME = @ servern -- Name of the input server

If (@ error <> 0 or @ returncode <> 0) goto quitwithrollback

End

Commit transaction
Goto endsave

Quitwithrollback:
If (@ trancount> 0) begin
Rollback transaction
Return 1
End
Endsave:

-- Star the job immediately
Exec @ returncode = MSDB. DBO. sp_start_job @ job_id = @ jobid

-- Return to the invoker immediately
Return @ returncode

 

Go

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.