Use SQLCMD to run TSQL commands

I did this when I was testing the 'Last Run Status' monitor in the SQL 2016 mp and SSMS was running slow.

Create a dummy job in SQL to run every few minutes and add this to the script section so the job executes successfully:

select @@version

Create a file called C:\temp\command.sql and add this:

USE msdb ;  
GO  
  EXEC dbo.sp_update_jobstep  
    @job_name = 'Test SQL agent job',  
    @step_id = 1,  
    @command =  'select * from this_will_break_it';  
GO 

Run it with this command:

SQLCMD -S scom2016 -i command.sql

You should get this returned:
Changed database context to 'msdb'.

You should see the health state of the job change (you won't get an alert). Update the file with select @@version and run it again to go back to healthy.

Comments