创建SQL作业错误的解决方法(不能将值 NULL 插入列 'owner_sid',表 'msdb.dbo.sysjobs';列不允许有空值。)
在用SQL语句创建SQL Server作业时有时出现如下错误:
- 消息 515,级别 16,状态 2,过程 sp_add_job,第 137 行
- 不能将值 NULL 插入列 'owner_sid',表 'msdb.dbo.sysjobs';列不允许有空值。INSERT 失败。
- 语句已终止。
这可能与为作业创建的数据库登录ID有关,这个登录ID需要是数据库的所有者(我的是sa),因此将
- @owner_login_name=N'HYSERITC003/wellcomm',
- 中的N'HYSERITC003/wellcomm'改为数据库的所有者,如sa就可以了。
- USE [msdb]
- GO
- /****** 对象: Job [24日SOX催办] 脚本日期: 11/25/2008 09:40:05 ******/
- BEGIN TRANSACTION
- DECLARE @ReturnCode INT
- SELECT @ReturnCode = 0
- /****** 对象: JobCategory [[Uncategorized (Local)]]] 脚本日期: 11/25/2008 09:40:06 ******/
- 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'24日SOX催办',
- @enabled=1,
- @notify_level_eventlog=0,
- @notify_level_email=0,
- @notify_level_netsend=0,
- @notify_level_page=0,
- @delete_level=0,
- @description=N'24日控制点负责人没填报控制点信息,短信催办。',
- @category_name=N'[Uncategorized (Local)]',
- @owner_login_name=N'HYSERITC003/wellcomm', --将'HYSERITC003/wellcomm'改为‘sa’。
- @job_id = @jobId OUTPUT
- IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
- /****** 对象: Step [24日催办] 脚本日期: 11/25/2008 09:40:07 ******/
- EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'24日催办',
- @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'-------【SOX】24号早上执行,短信催办控制点填报--------
- declare @Principal varchar(50)
- if exists (select distinct Principal from T_SOX where AuditingStatus<1 and Convert(varchar(7),writetime,120)=Convert(varchar(7),getdate(),120))
- declare cur_principal cursor for select distinct Principal from T_SOX where AuditingStatus<1 and Convert(varchar(7),writetime,120)=Convert(varchar(7),getdate(),120)
- open cur_principal
- fetch next from cur_principal into @Principal
- while (@@FETCH_STATUS =0)
- begin
- declare @uid varchar(50)
- select @uid=USER_ID From T_USER Where FULL_NAME=@Principal
- insert into t_sms_interface(wf_id,wfi_id,form_id,serial_no,receive_user_id,sms_content,create_time)
- values(''0'',''1'',''SOX控制点填报'',''1'',@uid,''今天是24号了,请到业务管理系统填写SOX控制点信息。谢谢。'',getdate())
- fetch next from cur_principal into @Principal
- end
- close cur_principal
- deallocate cur_principal
- ----------------------------------------------',
- @database_name=N'hyBizAs',
- @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'24日早上8点执行',
- @enabled=1,
- @freq_type=16,
- @freq_interval=24,
- @freq_subday_type=1,
- @freq_subday_interval=0,
- @freq_relative_interval=0,
- @freq_recurrence_factor=1,
- @active_start_date=20080728,
- @active_end_date=99991231,
- @active_start_time=80000,
- @active_end_time=235959
- 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: