UPDATE TOP with WHERE clause

Posts   
 
    
Robert.W
User
Posts: 79
Joined: 19-Jun-2007
# Posted on: 11-Sep-2009 12:57:01   

Hi, trying to come up with this SQL query using LLBLGen API:

declare @date datetime declare @max int declare @owner int

update top (@max) Timers set StateId = 2 where StateId = 1 and CreationDate < @date and OwnerId = @owner

Any help would be appreciated.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 11-Sep-2009 13:26:43   

declare @date datetime declare @max int declare @owner int

update top (@max) Timers set StateId = 2 where StateId = 1 and CreationDate < @date and OwnerId = @owner

You have 2 options:

1- Fetch the entities you want to update first and then update them.

2- Re-write the query as follows:

update Timers set
    StateId = 2
where
     id IN ( Select top (@max) id
     From Timers
     Where
     StateId = 1
     and CreationDate < @date
     and OwnerId = @owner)

And the IN predicate can be implemented using a FieldCompareSetPredicate.

Robert.W
User
Posts: 79
Joined: 19-Jun-2007
# Posted on: 11-Sep-2009 15:07:35   

I think I made it work, thanks.