Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

SQL Server (TSQL) - Is it possible to EXEC statements in parallel?

SQL Server 2008 R2

Here is a simplified example:

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)'

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

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.

What I am trying to do is:

  • Get a record
  • Lock it for a period of time
  • while it is locked, execute some other statements against this record and the table itself

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, there is a way, see Asynchronous procedure execution.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...