当前位置: 代码迷 >> Sql Server >> 求一個JOB很簡單!解决思路
  详细解决方案

求一個JOB很簡單!解决思路

热度:73   发布时间:2016-04-27 17:41:54.0
求一個JOB很簡單!!!!!!!!!!!!!!!!!!!
把表B的字段id   插入到表A字段id

要求該操作在2007年6月3號中午11:00執行,1次就可以

要具體代碼,最好有註釋。

------解决方案--------------------
WAITFOR TIME '2007-06-03 11:00 '
INSERT A(id) SELECT id FROM B WHERE 条件
------解决方案--------------------

-- Script generated on 2007-6-1 11:28
-- By: YXZ\Administrator
-- Server: (local)

BEGIN TRANSACTION
DECLARE @JobID BINARY(16)
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
IF (SELECT COUNT(*) FROM msdb.dbo.syscategories WHERE name = N '[Uncategorized (Local)] ') < 1
EXECUTE msdb.dbo.sp_add_category @name = N '[Uncategorized (Local)] '

-- Delete the job with the same name (if it exists)
SELECT @JobID = job_id
FROM msdb.dbo.sysjobs
WHERE (name = N 'auto_execute ')
IF (@JobID IS NOT NULL)
BEGIN
-- Check if the job is a multi-server job
IF (EXISTS (SELECT *
FROM msdb.dbo.sysjobservers
WHERE (job_id = @JobID) AND (server_id <> 0)))
BEGIN
-- There is, so abort the script
RAISERROR (N 'Unable to import job ' 'auto_execute ' ' since there is already a multi-server job with this name. ', 16, 1)
GOTO QuitWithRollback
END
ELSE
-- Delete the [local] job
EXECUTE msdb.dbo.sp_delete_job @job_name = N 'auto_execute '
SELECT @JobID = NULL
END

BEGIN

-- Add the job
EXECUTE @ReturnCode = msdb.dbo.sp_add_job @job_id = @JobID OUTPUT , @job_name = N 'auto_execute ', @owner_login_name = N 'sa ', @description = N 'No description available. ', @category_name = N '[Uncategorized (Local)] ', @enabled = 1, @notify_level_email = 0, @notify_level_page = 0, @notify_level_netsend = 0, @notify_level_eventlog = 2, @delete_level= 0
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 'test ', @command = N 'insert 表A(id)
select id
from 表B ', @database_name = N 'master ', @server = N ' ', @database_user_name = N ' ', @subsystem = N 'TSQL ', @cmdexec_success_code = 0, @flags = 0, @retry_attempts = 0, @retry_interval = 1, @output_file_name = N ' ', @on_success_step_id = 0, @on_success_action = 1, @on_fail_step_id = 0, @on_fail_action = 2
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXECUTE @ReturnCode = msdb.dbo.sp_update_job @job_id = @JobID, @start_step_id = 1

IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

-- Add the job schedules
EXECUTE @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id = @JobID, @name = N 'time ', @enabled = 1, @freq_type = 1, @active_start_date = 20070603, @active_start_time = 110000
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

-- Add the Target Servers
EXECUTE @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @JobID, @server_name = N '(local) '
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

END
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:



------解决方案--------------------
企业管理器-> 工具-> 作业调度-> 选择T-SQL命令-> 选择数据库,输入插入语句,下一步-> 时间,选择运行一次...
  相关解决方案