Workaround for creating SQL job errors (cannot insert value NULL into column ' Owner_sid ', table ' msdb.dbo.sysjobs '; column does not allow null values.) )

Source: Internet
Author: User

Sometimes the following error occurs when you create a SQL Server job with an SQL statement:

    1. MSG 515, Level 16, State 2, procedure sp_add_job, line 137th
    2. Cannot insert value NULL into column ' owner_sid ', table ' msdb.dbo.sysjobs '; column does not allow null values. INSERT failed.
    3. Statement has been terminated.

This may be related to the database login ID created for the job, which needs to be the owner of the database (my is SA), so

    1. @owner_login_name=n' Hyseritc003/wellcomm ',
    2. The n ' Hyseritc003/wellcomm ' in is changed to the owner of thedatabase, such as SA.
    1. Use [msdb]
    2. GO
    3. /****** object: Job [24th Sox Reminders] Script date: 11/25/2008 09:40:05 ******/
    4. BEGIN TRANSACTION
    5. DECLARE @ReturnCode INT
    6. SELECT @ReturnCode = 0
    7. /****** object: Jobcategory [[[Uncategorized (Local]]]] Script Date: 11/25/2008 09:40:06 ******/
    8. IF not EXISTS (SELECT name from msdb.dbo.syscategories WHERE name=n' [Uncategorized (Local)] ' and Category_class=1)
    9. BEGIN
    10. EXEC @ReturnCode = msdb.dbo.sp_add_category @class=n' JOB ', @type =n' LOCAL ', @name =n' [Uncategorized ( Local)] '
    11. IF (@ @ERROR <> 0 OR @ReturnCode <> 0) GOTO Quitwithrollback
    12. END
    13. DECLARE @jobId BINARY (16)
    14. EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name =n' 24th Sox reminders ',
    15. @enabled = 1,
    16. @notify_level_eventlog = 0,
    17. @notify_level_email = 0,
    18. @notify_level_netsend = 0,
    19. @notify_level_page = 0,
    20. @delete_level = 0,
    21. @description =n' 24th Control point leader did not complete control point information, SMS reminders. ',
    22. @category_name =n' [Uncategorized (Local)] ',
    23. @owner_login_name =n 'hyseritc003/wellcomm ', - - will ' Hyseritc003/wellcomm ' Change to ' sa '.
    24. @job_id = @jobId OUTPUT
    25. IF (@ @ERROR <> 0 OR @ReturnCode <> 0) GOTO Quitwithrollback
    26. /****** object: Step [24th Reminders] Script Date: 11/25/2008 09:40:07 ******/
    27. EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @[email protected], @step_name =n' 24th reminders ',
    28. @step_id = 1,
    29. @cmdexec_success_code = 0,
    30. @on_success_action = 1,
    31. @on_success_step_id = 0,
    32. @on_fail_action = 2,
    33. @on_fail_step_id = 0,
    34. @retry_attempts = 0,
    35. @retry_interval = 0,
    36. @os_run_priority =0, @subsystem =n' TSQL ',
    37. @command =n '-------SOX 24th morning execution, SMS reminders Control Point--------
    38. DECLARE @Principal varchar (50)
    39. If exists (select distinct Principal from T_sox where auditingstatus<1 and Convert (varchar (7), writetime,120) =convert ( varchar (7), GETDATE (), 120)
    40. Declare cur_principal cursor for SELECT DISTINCT principal from T_sox where auditingstatus<1 and Convert (varchar (7) , writetime,120) =convert (varchar (7), GETDATE (), +)
    41. Open Cur_principal
    42. FETCH NEXT from Cur_principal to @Principal
    43. While (@ @FETCH_STATUS =0)
    44. Begin
    45. DECLARE @uid varchar (50)
    46. Select @uid =user_id from T_user Where [email protected]
    47. Insert into T_sms_interface (wf_id,wfi_id,form_id,serial_no,receive_user_id,sms_content,create_time)
    48. VALUES (' 0', '1',' Sox control points ', ' 1 ',@uid,' Today is number 24th, please fill in the Sox control point information in the business management system. Thank you. ", GETDATE ())
    49. FETCH NEXT from Cur_principal to @Principal
    50. End
    51. Close Cur_principal
    52. Deallocate Cur_principal
    53. ----------------------------------------------‘,
    54. @database_name =n' Hybizas ',
    55. @flags =0
    56. IF (@ @ERROR <> 0 OR @ReturnCode <> 0) GOTO Quitwithrollback
    57. EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
    58. IF (@ @ERROR <> 0 OR @ReturnCode <> 0) GOTO Quitwithrollback
    59. EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @[email protected], @name =n' 24th morning at 8 o ',
    60. @enabled = 1,
    61. @freq_type = 16,
    62. @freq_interval = 24,
    63. @freq_subday_type = 1,
    64. @freq_subday_interval = 0,
    65. @freq_relative_interval = 0,
    66. @freq_recurrence_factor = 1,
    67. @active_start_date = 20080728,
    68. @active_end_date = 99991231,
    69. @active_start_time = 80000,
    70. @active_end_time =235959
    71. IF (@ @ERROR <> 0 OR @ReturnCode <> 0) GOTO Quitwithrollback
    72. EXEC @ReturnCode = Msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N' (local) '
    73. IF (@ @ERROR <> 0 OR @ReturnCode <> 0) GOTO Quitwithrollback
    74. COMMIT TRANSACTION
    75. GOTO Endsave
    76. Quitwithrollback:
    77. IF (@ @TRANCOUNT > 0) ROLLBACK TRANSACTION
    78. Endsave:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.