views:

140

answers:

4

Given the following:

if object_id('MyTable') is null create table MyTable( myColumn int )

Is it not possible that two separate callers could both evaluate object_id('MyTable') as null and so both attempt to create the table.

Obviously one of the two callers in that scenario would fail, but ideally no caller should fail, rather one should block and the other should create the table, then the blocked caller will see object_id('MyTable') as not null and proceed.

On what can I apply exclusive lock, such that I'm not locking more than is absolutely required?

A: 

I don't think, you should be worrying about this.

DDL statements don't run under a transaction. Also, the 2nd caller will fail, if the table already was created by a call from the 1st caller.

shahkalpesh
somehow don't worry be happy that the 2nd caller will fail is little comfort. I would rather the 2nd caller to proceed without error
Ralph Shillington
@Ralph: Makes sense. How often does this procedure execute? Why would you want to create a table in the procedure? Is this a table, you will drop at the end of the procedure? If not, why not create a real table upfront? Please explain your scenario as to why you are doing such a thing?
shahkalpesh
+1  A: 

After your initial check, use a try catch when creating the table, and if the error is that the table already exists, proceed, if not, you have a bigger problem

Chad
A: 

I don't allow users to create tables. In general that's a bad practice. If they need to insert data, the table is already there. If you are worried about two people creating the same table are you also worried about whether their data is crossing? I don't know what your proc does but if it deos something like delte the records if the table exists and then insert, then you could have strange reults if two users were on at a the same time. In general though, if you are needing to creat ea table at run time , it is usually a sign that your design needs work.

HLGEM
-1 the answer ison the merits of the assumed design. However question is a matter of implementation detail.
Ralph Shillington
When we see a bad design, it is our responsibility to point out that the issue may be the design not just the implementation detail you are asking a question for.
HLGEM
@HLGEM In this instance, the system requirement is that the multithreaded service that uses this database may or may not be configured to record error information in an audit table.If configured to record this audit info, it is suppose to create the table. If not configured the error audit table should not exist because an existing but empty audit table may indicate that the system is operating normally and no errors are logged.In a nutshell that's the design requirement.
Ralph Shillington
A: 

Usually CREATE TABLE is run from setup and installation scripts and is unreasonable to expect installation scripts to allow for concurrent installation from separate connections.

I recommend you use a session scopped app lock acquired at the beginning of your install/upgrade procedure, see sp_getapplock.

Remus Rusanu