• Brandie Tarvin (4/29/2016)


    I hope someone can assist me. I'd like to know where I can find the code behind the SQL engine's "Script AS" choice in the right-click GUI menu. The thing you get when you right-click a job, proc, etc. and go to Script X As and get to choose DROP, CREATE, or DROP and CREATE.

    I'd like to build my own version of the DROP scripts for the jobs that use @job_name instead of @job_id. Because job_ids vary from environment to environment, I don't want to use that in the sp_delete_job statement. There are a lot of jobs to move from SQL 2k8 to SQL 2012 and it is taking me too much time to go through each one and update the job_id with the job name. Cut-n-paste is easy, yeah, but there are so many other tasks on my plate that I don't have the time for it.

    Plus my hands are starting to hurt.

    So any thoughts on how I can turn a

    IF EXISTS (SELECT job_id FROM msdb.dbo.sysjobs_view WHERE name = N'MyJobName')

    EXEC msdb.dbo.sp_delete_job @job_id=N'67a26c58-8341-42a7-8773-b09ee0aef10f', @delete_unused_schedule=1

    into a

    IF EXISTS (SELECT job_id FROM msdb.dbo.sysjobs_view WHERE name = N'MyJobName')

    EXEC msdb.dbo.sp_delete_job @job_name=N'MyJobName', @delete_unused_schedule=1

    for lots of jobs at the same time?

    One idea is to create the scripts as usual and then do a REPLACE using the job name in msdb.dbo.sysjobs.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.