SQL Server (TSQL) - 是否可以并行执行 EXEC 语句? [英] SQL Server (TSQL) - Is it possible to EXEC statements in parallel?

查看:33
本文介绍了SQL Server (TSQL) - 是否可以并行执行 EXEC 语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SQL Server 2008 R2

SQL Server 2008 R2

这是一个简化的例子:

EXECUTE sp_executesql N'PRINT ''1st '' + convert(varchar, getdate(), 126) WAITFOR DELAY ''000:00:10'''
EXECUTE sp_executesql N'PRINT ''2nd '' + convert(varchar, getdate(), 126)'

第一个语句将打印日期并在继续之前延迟 10 秒.第二个语句应立即打印.

The first statement will print the date and delay 10 seconds before proceeding. The second statement should print immediately.

T-SQL 的工作方式是,在第一个语句完成之前不会评估第二个语句.如果我将其复制并粘贴到新的查询窗口,它将立即执行.

The way T-SQL works, the 2nd statement won't be evaluated until the first completes. If I copy and paste it to a new query window, it will execute immediately.

问题是我还有其他更复杂的事情要做,需要将变量传递给两个过程.

The issue is that I have other, more complex things going on, with variables that need to be passed to both procedures.

我想做的是:

  • 获取记录
  • 锁定一段时间
  • 当它被锁定时,对该记录和表本身执行一些其他语句

也许有一种方法可以动态创建几个作业?

Perhaps there is a way to dynamically create a couple of jobs?

无论如何,我正在寻找一种简单的方法来执行此操作,而无需手动打印语句并复制/粘贴到另一个会话.

Anyway, I am looking for a simple way to do this without having to manually PRINT statements and copy/paste to another session.

有没有一种无需等待/并行执行的方法?

Is there a way to EXEC without wait / in parallel?

推荐答案

是的,有办法,见异步过程执行.

但是,这可能不是您所需要的.T-SQL 是一种数据访问语言,当您考虑事务时,锁定和提交/回滚语义几乎不可能并行工作.例如,并行 T-SQL 与 请求队列一起工作,其中每个请求都是独立的,作业之间没有关联.

However, chances are this is not what you need. T-SQL is a data access language, and when you take into consideration transactions, locking and commit/rollback semantics is almost impossible to have a parallel job. Parallel T-SQL works for instance with requests queues, where each requests is independent and there is no correlation between jobs.

你所描述的听起来一点也不像是可以也不应该被并行化的东西.

What you describe doesn't sound at all like something that can, nor should, actually be paralellized.

这篇关于SQL Server (TSQL) - 是否可以并行执行 EXEC 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆