RE: select during transaction**

From: Pablo (Pablo_at_discussions.microsoft.com)
Date: 07/05/04


Date: Mon, 5 Jul 2004 01:26:01 -0700

Hi,

"RM" wrote:

> Hi
>
> I'm working with SQL server 2000 and have
> following problrm:
>
> in query analyzer I want to update
> one row of my table called 'books' in
> a transaction
>
> my table structure and data 'book' is :
> book_id(PK) book_desc
> ---------- ------------
> 1 physics
> 2 test
> 3 psychology
> 4 culture
>
> first page(in query analyzer):
> begin transaction
> update books set book_desc='mathematical'
> where book_id=2
>
> but imagine another user from another station want
> to see all the information about books during this changing
> ,which we can try it in another page in query analyzer.
> and I tried it in 6 different types and have got
> different results as followed,why?
> is there any way to see all old information as
> the result of first case below?
>
>
>
> second page(in query analyzer):
> 1)select * from books
>
> result: query is in working....

beacuse sql server decide on clustered index scan, therefore this statement must wait on releasing locks made by UPDATE
I don't know way for showing old data (i think is not possible) but with
  select * from books with (NOLOCK)
you can get new data, or
  select * from books with (READPAST)
you can get data without locked rows.

>
> 2)select * from books where book_id=1
>
> result: OK
> book_id book_desc
> ---------- ------------
> 1 physics

because sql server chose clustered index seek and it didn't touch/need
the data locking by UPDATE from first window.

>
> 3)select * from books where book_id=3
>
> result: OK
> book_id book_desc
> ---------- ------------
> 3 psychology
>
because sql server chose clustered index seek and it didn't touch/need
the data locking by UPDATE from first window.

> 4)select * from books where book_id<2
>
> result: OK
> book_id book_desc
> ---------- ------------
> 1 physics
>

because sql server chose clustered index seek and it didn't touch/need
the data locking by UPDATE from first window.

> 5)select * from books where book_id>2
>
> result: OK
> book_id book_desc
> ---------- ------------
> 3 psychology
> 4 culture

because sql server chose clustered index seek and it didn't touch/need
the data locking by UPDATE from first window.

>
> 6)select * from books where book_id<>2
>
> result: query is in working....
>
because sql server chose clustered index scan and was waited on locked resources.
When you change statement to

select * from books where book_id>2 or book_id<2

optimizer will choose clustered index seek and query will execute without blocking

>
>
>
>
>
> any help would be greatly thankful.
>
>

Regards,
Pablo



Relevant Pages

  • Re: SQL2005 sys tables
    ... Maybe you don't have the SQL Editor toolbar open. ... You can also change the database context by using the USE statement in the ... SQL Server Documentation Team ... I mean in sql2000 query analyzer I can do the select statement against ...
    (microsoft.public.sqlserver.security)
  • Summing tables in a UDF
    ... developed in Sybase into a SQL Server UDF. ... to run call the function in Query Analyzer even though it ... declare rentrev integer; ... What UDF type do I need to recreate this in SQL Server? ...
    (microsoft.public.sqlserver.programming)
  • Re: Using Query Analyzer with SQL 2005
    ... to 2005) was undone for the release of SP4. ... What version of SQL Server 2000 Query Analyzer are you using? ... or will I always need to keep a copy of SQL Server 2000 to install ...
    (microsoft.public.sqlserver.tools)
  • Re: Query Analyzer Erorr: Resource is low. Some results are dropped
    ... What exact build are you running? ... font or display resolution have any effect)? ... >When I use Query Analyzer on my server to either query the local SQL Server or a remote SQL Server, ...
    (microsoft.public.sqlserver.server)