SQL Server 2008 : Re-runnable stored procedure - sql

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

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

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

Execute SQL task with T-SQL query and with stored procedure in SSIS

In SSIS Execute SQL Task currently I am calling a stored procedure and inside the procedure I have a MERGE statement.
Is there any difference if I call that query (T-SQL MERGE) directly in the Execute SQL Task?
(are there any differences like Log will create if we used SP?)
Please reply me...
Thanks in advance
Stored procedure will provide you with a more maintainable solution as you will be able to leverage code reuse and there will be no need to change / re-release the package if your query logic changes
A stored procedure is also likely to provide you with the fastest execution time as it will be compiled and the execution plan will be reused on subsequent runs

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

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.