tags:

views:

50

answers:

2

I ran this query on my SQL Server database:

begin transaction;
insert into [db].[a].[Table1] ( [IsDeleted], [itemId], [regionId], [deskId], [LastUpdated], [LastUpdatedby])
select  [IsDeleted], [itemId], [regionId], 11, [LastUpdated], [LastUpdatedby]
from [db].[a].[Table1]
where deskId = 12;

update [db].[a].[Table1]
set deskId = 3
where deskId = 12;
commit transaction;

there are records that should have changed but it said (O rows affected).

i now went to do a:

Select * from [db].[a].[Table1]

and its just spinning with "Executing Query" for minutes now with no end in site.

  1. Does anyone see anything wrong with my first query that would cause some type of lock issue.
  2. Also, now that i am in this state, how do i get out of it?
+2  A: 

Is the first script still actually executing the Update Statement? If so this is to be expected as you are selecting rows that are not yet committed.

If not and it is apparently idle then I don't think you can have run the whole of the first script. At any rate it seems the transaction is still open and needs to be rolled back or committed.

You can confirm this by running select * from sys.dm_os_waiting_tasks and looking at the blocking_session_id or if you are on SQL2000 you would need to use exec sp_who2

Is this in management studio? If so go to the window with the insert statement and (in the case that the Update isn't still in progress) run either rollback or commit as desired.

If you select some text in SSMS and then run it will just execute the selected text. Which might explain how only part of the script could be run.

Martin Smith
@Martin Smith - i get an error: The user does not have permission to perform this action. Any other suggestions ?
ooo
Need more details? Is the query still running? Are both queries executed in SSMS? Are you a sysadmin on the server? What command did you run when you got the permissions error?
Martin Smith
@Martin Smith - the first query above definately finished as it return (0 rows affected) (which is not correct but thats another story). how can i go in and remove some lock that got stuck . . (as per the other answer, if i put with(nolock) it works fine
ooo
@ooo The `0 rows affected` would have been only for the insert statement. There should have been another message for the update part (unless it is still running). I guess the permissions bit in your comment was when you tried to look at `sys.dm_os_waiting_tasks` not when you tried to rollback.
Martin Smith
In management studio if it is still executing it will say executing in the status bar and the execute button will be disabled and the cancel button will be enabled. Is this the case for you at the moment on your original query window?
Martin Smith
@Martin Smith - there was one open window with uncommitted transactions. once i closed it, problem went away
ooo
Good stuff. Glad you got it sorted!
Martin Smith
@ooo try "EXEC sp_who2" I believe you should be able to see transactions that you have initiated that may be blocking (since you don't appear to have elevated privileges), otherwise you'll need to talk to someone that has sysadmin privileges to help you resolve the blocking issue.
crosan
Here's another statement that might help when you have this problem: SELECT @@TRANCOUNT. The result will show the number of active transactions in the current connection. If the result is greater than 0 then there are active transactions that may be blocking other sessions.
bobs
+2  A: 

If you are updating the first table still, then the read will not work; if you stop the query (first one) it will roll back, but that might take some time.

Change the select to this:

Select * from [db].[a].[Table1] WITH(NOLOCK)

And it should return right away.

Hope that helps

Kieren Johnstone
@Kieren Johnstone - that query did work but I still want to get out of this "state" that i am in because removing with(nolock) returns some rows but then just gets stuck
ooo
You have to commit or rollback the active transaction; it must still be active or running somewhere. Or, use Activity Monitor to determine what is locking (while the select is running without with(nolock))
Kieren Johnstone
@Kieren Johnstone - dont seem to have permission to run activity monitor
ooo
Could you read the whole comment please - you have to commit or rollback the active transaction; it must still be active or running somewhere.
Kieren Johnstone