มี SQL Server Agent ไหมครับ สามารถทำเป็น Job ได้ แต่ถ้าไม่มีอาจจะต้องใช้พวก Windows Task Schedule บน Windows แทนครับ อาจจะเขียนเป็น EXE แล้วสั่งให้รันทุก ๆ เวลาที่กำหนดครับ
Code (VB.NET)
' Table object used in iteration over Tables collection.
Dim oTable As SQLDMO.Table
Dim oJob As New SQLDMO.Job
Dim oJobStep As SQLDMO.JobStep
Dim idStep As Integer
' Create the SQL Server Agent job. Job will perform an update
' of all optimizer-supporting data distribution statistics.
oJob.Name = "Northwind_Statistics_Update"
oSQLServer.JobServer.Jobs.Add oJob
' Alter the job, adding job steps and setting starting step.
oJob.BeginAlter
' Each JobStep contains the Transact-SQL command batch
' updating statistics for a table.
idStep = 0
For Each oTable In oSQLServer.Databases("Northwind").Tables
' Only applies to user defined tables....
If oTable.Attributes <> SQLDMOTabAtt_SystemObject Then
Set oJobStep = New SQLDMO.JobStep
idStep = idStep + 1
oJobStep.Name = "Northwind_Statistics_Update_Step_" & idStep
oJobStep.StepID = idStep
oJobStep.DatabaseName = "Northwind"
oJobStep.SubSystem = "TSQL"
' TSQL uses the [] syntax to quote table identifers.
oJobStep.Command = "UPDATE STATISTICS [" & oTable.Name & _
"] WITH FULLSCAN, NORECOMPUTE"
' Default logic. Amended below.
oJobStep.OnFailAction = SQLDMOJobStepAction_QuitWithFailure
oJobStep.OnSuccessAction = SQLDMOJobStepAction_GotoNextStep
oJob.JobSteps.Add oJobStep
End If
Next oTable
' Reset the logic flow for the last job step to indicate success.
oJob.JobSteps.ItemByID(idStep).OnSuccessAction = _
SQLDMOJobStepAction_QuitWithSuccess
' Set the starting step for the job.
oJob.StartStepID = 1
' Alter the job.
oJob.DoAlter