Search code examples
sql-serverparallel-execution

Can you run 2 SQL queries in parallel **IN A SINGLE SSMS TAB**?


Not a dupe of any of these:

Since those relate to how to execute parallel queries from particular language. To which the answer is naturally "just fire off the queries in parallel, in whatever manner your language has for parallel execution".

One answer on the above comments that one way to have 2 processes, is that you can open 2 SSMS instances, or tabs, and run those queries in parallel. That's also not what I'm asking.


I want to know if there's any way to write SQL in a single SSMS tab, such that with a single execution (i.e. pressing F5 once) I can run 2 queries in parallel?

By analogy, how can I run a query 5 times, in a single SSMS tab, without copy-pasting it? Would either get the answer: "Look into the SQL WHILE loop", or "use GO 5". Not "here's how your write a for loop in C#".


Solution

  • One solution to fire off 2 queries at the same time would be to use a maintenance plan. The maintenance plan could contain a job with several nodes (not connected). Each node will be executed in parallel when the job is executed.

    How to triggering the maitenance plan job from a query has already been answered here.

    PRO's

    • Statements are triggered at the same time.
    • Maintenance plan execution can be triggered periodically with a schedule.

    CON's

    • Output of the queries is not directly visible. You would have to store the output and query the results in a new query.