How can I run sql server stored procedures in parallel? - sql

I want to do something like:
exec sproc1 and sproc2 at the same time
when they are both finished exec sproc3
I can do this in dts.
Is there a way to do it in transact sql?
Or is there a way to do it with a batch script (eg vbs or powershell)?

You could create a CLR Stored Procedure that (using C#) would call the first two on their own threads, and then block until both are complete... then run the third one.
Are you able to use CLR sprocs in your situation? If so, I'll edit this answer to have more detail.

sp _ start _ job
I'm doing a similar thing at the moment, and the only way I've found to avoid using SSIS or some external shell is to split my load routine into 'threads' manually, and then fire a single master sqlagent job which in turn executes as many sp _ start _ job's as I have threads. From that point, they all run autonomously.
It's not exactly what we're looking for, but the result is the same. If you test the job status for the sub jobs, you can implement your conditional start of sproc 3 as well.
What's the point in 8 cores if we can't use them all at once?

Do you absolutely need both SPs to be executed in parallel?
With simple CRUD statements within a single SP, I've found SQL S. does a very good job of determining which of them can be run in parallel and do so. I've never seen SQL S. run 2 SPs in parallel if both are called sequentially from a T-SQL statement, don't even know if it's even possible.
Now then, do the DTS really execute them in parallel? It could be it simply executes them sequentially, then calls the 3rd SP after the last finishes successfully.
If it really runs them in parallel, probably you should stick with DTS, but then I'd like to know what it does if I have a DTS package call, say, 10 heavy duty SPs in parallel... I may have to do some testings to learn that myself :D

You can use SSIS. The benefits of this are that the package can be stored in the SQL Server and easily scheduled there.
From PowerShell or just about any external scripting language, you can use the SQL command line osql or sqlcmd. This technique can also be used to schedule it on the SQL Server by shelling out using xp_cmdshell also.

Related

SQL Server stored procedure or alternative method to restart SQL Server Agent

There are various ways to restart the SQL Server Agent on a server, but I would like to do it from a stored procedure in one of my databases (on the same server). How would one go about doing that? Is there some sort of a system stored procedure that I could call? Or would I need to call some sort of third party library/external language to accomplish that such as the following?
Apparently one of my co-workers had already solved this:
EXEC xp_servicecontrol N'STOP',N'SQLServerAGENT';
-- Give the service a little time to stop
waitfor delay '00:00:10.000'
EXEC xp_servicecontrol N'START',N'SQLServerAGENT';
Not sure this the best solution, but should work for my purposes...
PowerShell could be a good approach depending on the circumstances. the Start-Service and Restart-Service commands would be a good starting point :O

how to run sql procedure as a job that can be scheduled

I have a stored procedure that I want to have run every day. I have never used jobs or schedules so i'm not sure how to do this. I'm using sql server 2012 management studio.
It is pretty straight forward. Here is an overview for creating a job from MSDN.
Ultimately you just create the job and your step is to EXEC yourStoredProc.
You can then create a schedule for your job to run whenever (link at the bottom of above article).
If it's a simple stored procedure, try the sqlcmd
http://msdn.microsoft.com/en-us/library/ms162773%28v=sql.110%29.aspx
which you can schedule in the Windows Task Scheduler. Be careful in setting up the service account it will run as, it need permissions on the database.
If it's more complex, setting up a package in SSIS (SQL Server Integration Services) gives a huge degree of power and flexibility.

Simple SQL Server Profiler Event Advice

I have to debug a long running SQL Server stored procedure which dynamically builds and executes queries at runtime. I want to run a trace so I can see all of the individual pieces of SQL that are executed.
Which events will provide that level of detail?
I've been advised to use Stored Proc: RPC Completed; Batch Completed and Batch Started. But that's only bringing back the initial execution statement. ie. literally "exec MyStoredProcName".
Any ideas?
Thanks In Advance.

Start SSIS Asynchronously from a stored proc

I need to start a SSIS package via a stored procedure. I chose to use 'exec dtexec' instead of starting a job to launch the package, so I can have the ability to set variable in the package. My problem is that I need the package to run asynchronously so that the stored procedure will return instead of it hanging or timing out.
What is the best practice for achieving this?
If you want Async operation with variables, I would construct a table to hold the variables you wanted to pass in, and an Agent job launched SSIS with them. Then you could use sp_start_job to launch that job asynchronously. The package could read the variables it needed from the table, or the job could read them to construct an appropriate launch command. The package could even update the table to indicate results back to BizTalk.
Use the stored proc to start a SQL Server Agent job that invokes the SSIS package...
You can't invoke directly from a stored proc (which is not a good idea anyway) and then have the stored proc terminate. You have to decouple the stored proc execution from the SSIS execution
Setting variables is easy though in SQL Server agent (GUI) and on command line (use /Set)

Is it possible to automate MS SQL Server (2005) queries via SQL Server Management Studio?

I have two queries saved on my SQL Server 2005 and I would like to have them running at certain intervals, e.g. every 24 hours. Is there a way to automate these queries and schedule them via SQL Server Management Studio?
You need to use the SQL Server Agent, installed as part of SQL Server. This is the background service that's responsible for running scheduled maintenance and backup tasks.
Expand the SQL Server Agent node in SQL Server Management Studio, you should see a tree node called "Jobs"
Right-clicking this will give you the option to add a new job. Jobs consist of a series of steps to be executed in order, and when you add a new step to your job, you can choose various types of step, including "Transact-SQL Script"
Create a new job, add a single T-SQL step, put the queries that you want to run into the step properties, and then use the "Schedule" option in the Job properties to create a recurring schedule that'll run the job every 24 hours (or whenever).
You can use the SQL Server Agent, which will allow the server to run the script/stored procedure.
You can also use the Windows Task Scheduler, either on the server or on any other server or workstation to schedule isqlw/sqlcmd to execute your script/stored procedure.
Create a job with a step in which you execute your queries; the job can be scheduled at your needs.
At a previous employer the operations department had a task sheduling application. They prefered to use the command line tools that come with sql server to execute jobs (stored procedures) on a scheduled basis. This way the "task scheduling application" could recieve an exit status (pass/fail, ok/error) and hold up dependent jobs.
I'm sorry I don't remember the name of the command line tool, but it was part of the sql server distro. I also do not remember the name of the task scheduling application. It was not the windows task scheduler. It was something enterprise level used to manage the nightly cycle.
Not sure of the scale of your project, but this is another way to go.
SKapsal's comment on a command line tool for executing SQL commands is a reference to osql (for SQL2000) or sqlcmd (for SQL2005+). You could save your script a file and run it from that command line using Windows Task Scheduler or something similar.
SQL Agent is still the preferred solution, however, as it provides GUI controls for job creation, scheduling, logging and viewing job execution history/results.
how to schedule a job for sql query to run daily?
This is similar question with helpful answer.
Covering simple step by step manual.