Launch multiple stored procedures to run 'in the background' on SQL Server Express Edition - sql

Is it possible to run multiple stored procedures that run 'in the background'?
The stored procedures must be launched from a single, master stored procedure, in the same way that multiple worker threads are spawned. For example:
CREATE PROCEDURE MyLauncher
AS
BEGIN
BEGIN
#EXEC MyBackgroundSP01 -- Runs in parallel to the other 2
#EXEC MyBackgroundSP02 -- Runs in parallel to the other 2
#EXEC MyBackgroundSP03 -- Runs in parallel to the other 2
END
END

It is possible in SQL 2005 and later. Have look at http://rusanu.com/2009/08/05/asynchronous-procedure-execution/

No this isn't possible as you have described it. You could run multiple SQL Jobs which will execute the procedures concurrently/

According to this question you could try using the Service Broker
Asynchronous Stored Procedure Calls

If you run they in the same procedure, it will launch in the same thread (and in the same internal transaction, what can make the log very big).

Not with pure T-SQL. But you could write a little dotNET app to run them asynchronously easily enough, as long as you leave the connection option until all three are finished.

IF both Stored procedure have same parameter then you can create a new store procedure
like
create third procedure
(#colname int)
as
begin
exec first procedure
exec second procedure
end
exec third procedure
You can try it. I am sure how appropriate it is.

Related

Using MSDB stored procedures in application's database stored procedure

This seems like it would be trivial, but I have not been able to come up with a solution to this small problem.
I am attempting to create a stored procedure in my application's database. This stored procedure just executes a job that has been set up in the SSMS on the same server (seemed to be the only way to programmatically execute these jobs).
The simple code is shown below:
USE ApplicationsDatabase
GO
CREATE PROCEDURE [dbo].[procedure]
AS
BEGIN
EXEC dbo.sp_start_job N'Nightly Download'
END
When ran as is, the procedure technically gets created but cannot be executed due to it not being able to find the 'sp_start_job' since it is using the ApplicationsDatabase. If I try to create the procedure again (after deleting previously created) but updating the USE to MSDB, it tries to add it to that system database for which I do not have permissions to do. Finally, I attempted to keep the original create statement but added the USE MSDB within the procedure (just to use the 'sp_start_job' procedure), but it would error saying USE statements cannot be placed within procedures.
After pondering on the issue for a little (I'm obviously no SQL database expert), I could not come up with a solution and decided to solicit the advice of my peers. Any help would be greatly appreciated, thanks!
You will have to fully qualify the path to the procedure. Of course, you can only execute this is the application has permissions.
Try this:
USE ApplicationsDatabase
GO
CREATE PROCEDURE [dbo].[procedure]
AS
BEGIN
EXEC msdb.dbo.sp_start_job N'Nightly Download'
END

How to prevent SQL Stored Procedure Alters while the stored procedure is running?

We have a stored procedure that runs hourly and requires heavy modification. There is an issue where someone will edit it while the stored proc is running and will cause the stored proc to break and end. I am looking for an error to pop up when someone tries to edit a stored procedure while it is running, rather than breaking the execution.
It's a sql server agent job that runs hourly, I get "The definition of object 'stored_procedure' has changed since it was compiled."
Is there something I can add to the procedure? A setting?
I think you can use a trigger at the database level in order to prevent changes and within the object apply validations for the running stored procedure, something like this:
USE [YourDatabase]
GO
ALTER TRIGGER [DDLTrigger_Sample]
ON DATABASE
FOR CREATE_PROCEDURE, ALTER_PROCEDURE, DROP_PROCEDURE
AS
BEGIN
IF EXISTS (SELECT TOP 1 1
FROM sys.dm_exec_requests req
CROSS APPLY sys.dm_exec_query_plan(req.plan_handle) sqlplan WHERE sqlplan.objectid = OBJECT_ID(N'GetFinanceInformation'))
BEGIN
PRINT 'GetFinanceInformation is running and cannot be changed'
ROLLBACK
END
END
that way you can prevent the stored procedure being changed during execution, if it's not being executed changes will be reflected as usual. hope this helps.
You should do some research and testing and confirm this is the case. Altering a SProc while executing should not impact the run.
Open two SSMS windows and run query 1 first and switch to window 2 and run that query.
Query 1
CREATE PROCEDURE sp_altertest
AS
BEGIN
SELECT 'This is a test'
WAITFOR DELAY '00:00:10'
END
GO
EXEC sp_altertest
Query2
alter procedure sp_altertest AS
BEGIN
SELECT 'This is a test'
WAITFOR DELAY '00:00:06'
END
GO
Exec sp_altertest
Query 1 should continue to run and have a 10 sec execution time while query 2 will run with a 6 sec runtime. The SProc is cached at the time of run and stored in memory. The alter should have no impact.

Wait for the stored procedure to wait for the execution of other stored procedure

How can I run SQL Server stored procedures one after another in sequence?
I would like the procedure to wait for termination before the next one is executed.
I am using SQL Server 2008
Do you mean like described here?
http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/00ab0007-e46f-4047-b758-e0ab1c56e84c

How to execute multiple stored procedures at a time?

I want to know that is there any way through which I can execute all my stored procedure at a time.
Presently I am executing each stored procedure using exec "Stored Procedure name" command.
And I have more than 200 stored procedure to be executed on my database.
Is there any easy way out to execute all these stored procedure at a single time as it is difficult for me to keep a track of them?
I suggest you write a stored procedure which calls the other ones.
Put all stored procedures inside a stored procedure,
CREATE PROCEDURE CallAllProcedure
AS
BEGIN
CALL Proc1
CALL Proc2
END
Assuming that you are using Query Analyzer, just put a GO in between all those stored proc and run script.
If you want to execute them all in parallel you could create a SQLJob and schedule them all to execute at the same time. Link below is general usage of SQL Jobs.
http://msdn.microsoft.com/en-us/library/ms190268.aspx
you can select all stored procedure names from sys.objects table querying type='P'. After you can use cursor for every stored procedure name to execute. But how about stored procedures with parameters? you must provide parameter values as well to avoid from errors.
You can use Service broker to do this async but I dont think it is a great idea to run 200 stored procs at the same time unless you are sure there will not be any contention on the DB

SQL Server 2008 : Re-runnable stored procedure

I want to make my stored procedure re-runnable.
If it started and run partial way and failed because of a reason, it should be re-runnable.
Please suggest some ways to do that.
Thanks !
You can create a JOB and trigger it in the stored procedure.
Specify the retry times and the retry interval on the Step (work) that you want to re-run.
here's a reference on how to start a Job.
http://www.mssqltips.com/sqlservertip/1730/different-ways-to-execute-a-sql-agent-job/
If you just need to execute your stored procedure from beginning after failure, you can catch the failrue (using TRY .. CATCH or ##ERROR) and then redirect the execution to the begin of the stored procedure using GOTO