views:

70

answers:

2

If I have several SPs

SP1
SP2
some_inline_queries

how do I ensure that they are all run at once without interruption from other threads? Is it possible to do this from SQL Server level?

edit:

Let's say we have a main script with 3 major actions:

sp1 scans table t1 to generate a random string that is unique to column c1 in t1;

sp2 does some fairly intensive stuff;

some statement inserts the random string returned by sp1 into c1 of table t1. So if I run many instances of this main script simultaneously,I need all contents in t1.c1 to be distinct when all scripts finished running.

+1  A: 

are you running them inside a transaction? Not sure what you mean by "interruption" but they would be safe assuming that they are within a:

Begin Transaction MyTranNameHere
exec sp1
exec sp2
some statement
Commit Transaction MyTranNameHere
NTulip
let me know what you mean and maybe I could answer it better.
NTulip
Please see edit in OP. Thanks for the help.
Haoest
+1  A: 

If I'm understanding you right, you want to set the transaction isolation level to SERIALIZABLE. Assuming MSSQL has true serializability (it might not; true serializability is rarely needed and often quite costly to implement), this will guarantee that even if you execute many transactions at once, the end result will be identical to executing one (though which one is usually non-deterministic), waiting for it to finish, then executing another transaction, and so on. Be careful, though: there are often subtle "bugs", be they actual bugs or misfeatures, in database SERIALIZABLE implementations, since this stuff is really tricky to get right. Especially nasty is the fact that some MVCC-based databases (Oracle and PostgreSQL use MVCC, and I know the Postgres lists were recently discussing these issues with their DBMS) don't really implement SERIALIZABLE perfectly, going instead for what should be called SNAPSHOT isolation -- this gives 99% of the benefits of SERIALIZABLE with minimal performance hit, but that's no good if you fall into that 1%.

If SERIALIZABLE is not an option, either because it doesn't do what you want or for some other reason, you can always have each SP take an exclusive lock before doing its dirty work. This might lead to deadlocks or timeouts and require other tuning elsewhere, so it's kind of an ugly option, but it should do the job.

kquinn