I have the following query that generates the commands necessary for me to disable all the SQL Agent jobs particular to a category.
Select 'Exec MSDB.dbo.sp_update_job @job_name = N''' + SJ.Name + ''', @Enabled = 0'
FROM msdb..sysjobs SJ inner join msdb..syscategories SC
on SJ.category_id = SC.category_id
Where SJ.[Enabled] = 1
and SC.Name = 'BI ETLs'
How can I automate this totally, so it will generate the commands and execute them without it being a manual process?
You could use dynamic SQL:
With
SQL Server 2017
it is much easier to concatenate strings usingSTRING_AGG
: