开发者

how to start a sql server agent job from java code

I have few SQL Server Agent Jobs running in my project. The jobs run perfectly as scheduled, no issues.

But now I need to be able to start these jobs from the front end (Like on a click of button or so).

How can I do it ?

Do these jobs behave just like a functi开发者_C百科ons ?


You can do this with any db connector I've tried--here are a couple examples...

Using CallableStatement:

Connection rConn = //however you get your connection...
CallableStatement cs = rConn.prepareCall("EXEC dbo.sp_start_job N'your job name'");
boolean checkvar = cs.execute();

Alternatively, if you use a jdbc template:

jdbcTemp = //however you get your template...
jdbcTemp.update("EXEC msdb.dbo.sp_start_job N'" + procName + "'");

Also, you will likely need to adjust the permissions of the msdb in order for this to work. Your account needs to either be a sysadmin or have the SQLAgentOperatorRole role. To set this in SQL Server Management, go to Security under your db engine, expand logins, right click on the account you will use and select properties. Under Server Roles you can grant sysadmin, or under User Mapping check msdb, then select TargetServersRole and SQLAgentOperatorRole from the list below.

hth


you can call it by using the sp_startjob proc

example

EXEC msdb.dbo.sp_start_job N'MyJobName';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜