使用2000创建临时Job异步执行SQL脚本

来源:互联网 发布:nx软件下载 编辑:程序博客网 时间:2024/05/18 22:12
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

应用场景:
   在庞大的中很多复杂的更新查询非常的耗时。为了避免用户长时间的等待,那些耗时的操作可以采用异步执行的方法,立刻返回执行信息给用户,同时在数据库后台执行操作,等到执行完毕再更新数据表。
   
开发环境:
SQLSERVER2000

解决方案:
   在SQLSERVER2000中创建一个临时的Job,(或者固定的Job,根据具体的应用场景),传递需要执行的SQLbatch脚本,然后启动这个Job。这样就可以在数据库获得异步调用的功能了。由于创建的是临时的Job
SQLSERVER在该Job运行结束以后会自动删除该Job

缺点:该存储过程必须指定数据库的名字

====================================================================================
/******************************************************************************
 *Author:iret
 *Desc: CreatetemporaryJobtoprovide asynchronouslyinvokingSQLbatch
 *           在SQLSERVER2000中创建用于执行异步调用的临时Job
 * @EXECSQL: Transact-SQL batch
 * Eample:EXECdbo.AsynchronousInvoking@EXECSQL= 'UPDTAEcustomer SETbalance=0'
 *缺点:该存储过程必须指定数据库的名字
 *ModifiedDate:2004/11/03
 ******************************************************************************/
CREATEProceduredbo.AsynchronousInvoking
 @EXECSQLnvarchar(4000)
AS

BEGINTRANSACTION           
 DECLARE@JobIDBINARY(16) 
 DECLARE@ReturnCodeINT   
 SELECT@ReturnCode=0    

BEGIN

 --AddtheJob
 EXECUTE@ReturnCode=msdb.dbo.sp_add_Job@Job_id=@JobIDOUTPUT,
    @Job_name=N'temp_SQLJob',
    @owner_login_name=N'',
    @description=N'descriptionforJob',--thedescriptionof theJob 
     @category_name=N'[Uncategorized(Local)]',
    @enabled=1,
    @notify_level_email=0,
    @notify_level_page=0,
    @notify_level_netsend=0,
    @notify_level_eventlog=0,
    @delete_level=3
    
 IF(@@ERROR<>0OR@ReturnCode<>0)GOTOQuitWithRollback

 --AddtheJobsteps
 EXECUTE@ReturnCode=msdb.dbo.sp_add_Jobstep@Job_id=@JobID,
    @step_id=1,
    @step_name=N'step1',
    @command=@EXECSQL,--SQL batch
    --缺点:该存储过程必须指定数据库的名字
     @database_name=N'your_database_name',--the databasename oftheJobtomanipulate
     @server=N'',
    @database_user_name=N'appuser',
    @subsystem=N'TSQL',
    @cmdexec_success_code=0,
    @flags=0,1
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>