views:

201

answers:

5

I have a simple query like this

SELECT * FROM MY_TABLE;

When I run it, SQL Server Management Studio hangs.

Other tables and views are working fine.

What can cause this? I've had locks while running UPDATE statements before, and I know how to approach those. But what could cause a SELECT to lock?

I have run the "All Blocking Transactions" report, and it says there are none.

+2  A: 

use SELECT * FROM MY_TABLE with (NOLOCK)

Mikhail
That works, but how can I tell who/what is causing the lock?
JosephStyons
See answer from BradC:Some other process has a lock on the table, preventing your select from going through until that lock is released
Mikhail
+4  A: 

It is probably not the select that is locking up, but some other process that is editing (udpate/delete/insert) the table that is causing the locks.

You can view which process is blocking by runing exec sp_who2 on your SQL Server.

Alternatively, if you are OK with dirty reads, you can do one of two things

SELECT * FROM Table WITH (NOLOCK)

OR

SET Transaction Isolation Level Read Uncommitted
SELECT * FROM Table
Raj More
exec sp_who2 gave me an ALTER INDEX that is in progress. Seems to be the culprit. Thanks.
JosephStyons
A: 

Two possibilities:

  1. Its a really massive table, and you're trying to return 500m rows.
  2. Some other process has a lock on the table, preventing your select from going through until that lock is released.
BradC
No to #1. Yes to #2 but who/what?
JosephStyons
+2  A: 

If there's a lot of other activity going on, something else might be causing locks, and your SELECT might be the deadlock victim. if you run the following

SELECT * FROM my_table WITH(nolock)

you're telling the database that you're ok to read dirty (uncomitted) data, and that locks caused by other activity can be safely ignored.

Also, if a query like that causes management studio to hang, your table might use some optimization

David Hedlund
A: 

MY_TABLE could be also locked up by some uncommitted transaction -- i.e. script/stored procedure running (of fails when run) in another MSMM window.

Grzegorz Gierlik