question

sreelatha16 avatar image
sreelatha16 asked

sp_stop_job when invoked from stored procedure is not working

Hi All,


I have created a stored procedure and passed jobname to sp_stop_job. This stored procedure is invoked by a SSMS Job. Though the job is running for a long time, sp_stop_job is not stopping the job . But when the same statement (Exec sp_stop_job ) is executed outside of the stored procedure, it is stopping the stuck job.

It looks like a permission issue and please help with the persmissions to be granted to Stored proc/Job/UDF.


SET @Jobname = (SELECT JobName from UDF()

WHERE StatusFlag = 'Stuck'

runtime>= Threshold)

DECLARE @dynSql NVARCHAR(MAX) = ''

SELECT @dynSql += N' Exec msdb.dbo.sp_stop_job ''' + @Jobname + N''''+ CHAR(10) + CHAR(13)


stored-proceduressqlserver2012
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

minivike avatar image
minivike answered

You're missing an AND in your WHERE clause. And all you're doing with the @dynSql variable is creating a string. I would eliminate the last 2 lines and replace them with

Exec msdb.dbo.sp_stop_job @Jobname

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

minivike avatar image minivike commented ·

Additionally, if you expect more than one result from your top query, you could wrap this in a Cursor.

0 Likes 0 ·
@Sree avatar image
@Sree answered

Thanks a lot. It helped me to solve the issue

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.