博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
引锁,重建任务
阅读量:5955 次
发布时间:2019-06-19

本文共 3011 字,大约阅读时间需要 10 分钟。

当使用数据库管理功能下的维护计划,建立索引失败的情况下,使用代理执行作业的方式,比较实用的,代码如下:

方法1:
USE [msdb]
GO

/** Object: Job [索引重建] Script Date: 2018-2-8 16:29:40 **/

BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/** Object: JobCategory [[Uncategorized (Local)]]] Script Date: 2018-2-8 16:29:40 **/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

END

DECLARE @jobId BINARY(16)

EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'索引重建', 
@enabled=1, 
@notify_level_eventlog=0, 
@notify_level_email=0, 
@notify_level_netsend=0, 
@notify_level_page=0, 
@delete_level=0, 
@description=N'无描述。', 
@category_name=N'[Uncategorized (Local)]', 
@owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/** Object: Step [索引] Script Date: 2018-2-8 16:29:40 **/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'索引', 
@step_id=1, 
@cmdexec_success_code=0, 
@on_success_action=1, 
@on_success_step_id=0, 
@on_fail_action=2, 
@on_fail_step_id=0, 
@retry_attempts=0, 
@retry_interval=0, 
@os_run_priority=0, @subsystem=N'TSQL',@command=N'/物料表索引/
ALTER INDEX [PK_BD_MATERIAL] ON [dbo].[T_BD_MATERIAL] REBUILD PARTITION = ALL WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)

ALTER INDEX [IDX_BD_MTRL_FMASTERID] ON [dbo].[T_BD_MATERIAL] REBUILD PARTITION = ALL WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)

ALTER INDEX [IDX_BD_MTRL_COMBIN] ON [dbo].[T_BD_MATERIAL] REBUILD PARTITION = ALL WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)', @database_name=N'数据库名称', @flags=0

IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'计划1', 
@enabled=1, 
@freq_type=4, 
@freq_interval=1, 
@freq_subday_type=1, 
@freq_subday_interval=0, 
@freq_relative_interval=0, 
@freq_recurrence_factor=0, 
@active_start_date=20180208, 
@active_end_date=99991231, 
@active_start_time=230000, 
@active_end_time=235959, @schedule_uid=N'4e5e768f-a2b5-4042-9766-ba8e5ebbd42e'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback: 
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:

GO

方法2:
右击索引,选择重新生成,打开数据库跟踪,找到生成语句 ,把代码粘贴到执行计划中,推荐使用这种方式

本文转自51GT51CTO博客,原文链接: http://blog.51cto.com/yataigp/2070269,如需转载请自行联系原作者

你可能感兴趣的文章
专业级的WPF条形码控件
查看>>
Python发布 - distutils简单使用
查看>>
Linux常用的基本命令08
查看>>
unzip直接覆盖解压
查看>>
IntelliJ IDEA打开错误 _CGContextSetAllowsAcceleration
查看>>
Office 365强势来袭PART3:管理云用户
查看>>
Powershell 函数中的CmdletBinding()是怎么回事?
查看>>
查看Office365迁移任务进度状态
查看>>
MySQL: ERROR13(HY000):Can't get stat of的问题
查看>>
WinCE应用程序产生Data Abort 错误分析
查看>>
Maven(一):Maven安装及Eclipse配置
查看>>
【码云周刊第 3 期】来自国内开发者的实战项目,开源让通讯从未如此简单!...
查看>>
Linux中,根目录下文件夹的含义
查看>>
知识应该是免费的
查看>>
我的友情链接
查看>>
11_css选择符类型2.html
查看>>
css布局模型(摘抄自慕课)
查看>>
学习 Message(11): 测试 TWMMouse 结构相关的鼠标消息
查看>>
STM32 IO口双向问题
查看>>
iOS APP删除系统相册中选中的图片
查看>>