0

I need to execute one procedure from another in background\asynchronously.

procedure1:

CREATE PROCEDURE procedure1
WITH EXECUTE AS OWNER
AS
BEGIN

  //something happens here

EXEC procedure2

  //something happens here
END

procedure2:

CREATE PROCEDURE procedure2
WITH EXECUTE AS OWNER
AS
BEGIN

 WAITFOR DELAY '00:02'
END

I need procedure1 to execute without waiting for procedure2 to end. I've read this answer in other post:

There was once I tried to achieve this by wrapping the stored procedure into Job, and then Calling the job in the procedure through sp_start_job system sp. EXEC dbo.sp_start_job N'Job name' ;

but I don't understand it. Can anyone explain it to me, please? Cause it doesn't look like something advanced but I cannot achieve what I want.

Artur
  • 47
  • 7

1 Answers1

1

They are referring to creating a SQL Agent Job. This is in its simplest a way to schedule scripts to be run on the server. Typically you would have scripts that need to be run every hour, day, week, etc and rather than run them manually you set up an Agent Job to run them automatically for you.

The Jobs all live in the SQL Server Agent section of your Object Explorer:

Once you have created the job, you can call it manually using sp_start_job. If you only want the job to run when you call it, simply create the job and don't assign a schedule.

iamdave
  • 12,023
  • 3
  • 24
  • 53
  • Thank you for your answer. Unfortunately I have an express edition of SQL Server so I can't use SQL Server Agent. – Artur Sep 08 '16 at 12:53
  • @Artur What are you using this SQL Server for? If it is just a personal project you can use the full featured Dev version of 2016 for free. – iamdave Sep 08 '16 at 14:01
  • @Artur While this is not an exact copy, the idea of running something in the 'background' for SQL it would be preferable to what iamdave is saying. A workaround for if you HAVE TO use SQL Express is running Windows Task Scheduler instead and making SQL batch scripts. There is a thread on SO here: http://stackoverflow.com/questions/7201061/how-to-create-jobs-in-sql-server-express-edition – djangojazz Sep 08 '16 at 14:41