SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Start SQL Agent job using stored procedure


Start SQL Agent job using stored procedure

Author
Message
todor.belchev
todor.belchev
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 85
Hi,

I'm trying to execute a stored procedure which will start a SQL Agent Job but I got "The SELECT permission was denied on the object 'sysjobactivity', database 'msdb', schema 'dbo'". I know that the db_owner can run the query below but this is not the case. I need the following query to be executed using a SQL Agent Proxy account but what permissions/roles this account should has?

DECLARE @SQLJobHistoryID bigint
DECLARE @Status nvarchar(100)
SET @Status = 'Running'

-- check if @Action is passed. If is not then get the default value - check
IF @Action IS NULL
BEGIN
SET @Action = 'check'
END
-- select last history id. if the job is running at the moment the id is NULL
SELECT TOP 1 @SQLJobHistoryID = activity.job_history_id
FROM msdb.dbo.sysjobs jobs
INNER JOIN msdb.dbo.sysjobactivity activity ON activity.job_id = jobs.job_id
WHERE jobs.name = @SQLJobName
ORDER BY activity.start_execution_date DESC

-- if @SQLJobHistoryID IS NULL than the job is running and we cannot run it again until the job finish
IF @Action = 'run' AND @SQLJobHistoryID IS NOT NULL
BEGIN
IF EXISTS ( SELECT * FROM msdb.dbo.sysjobs WHERE name = @SQLJobName)
BEGIN
EXEC msdb.dbo.sp_start_job @SQLJobName
SELECT @Status as [Status]
RETURN
END
END


SELECT
@Status = CASE
WHEN run_status = 0 THEN 'Failed'
WHEN run_status = 1 THEN 'Succeeded'
WHEN run_status = 2 THEN 'Retry'
WHEN run_status = 3 THEN 'Canceled'
END
FROM msdb.dbo.sysjobhistory history
WHERE history.instance_id = @SQLJobHistoryID

SELECT @Status AS [Status]


Thanks
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search