UPDATE with TOP

Posts   
 
    
slade52
User
Posts: 46
Joined: 15-Aug-2007
# Posted on: 09-Oct-2007 06:02:18   

Using SelfServicing, Is there a way to use the Collection class and UpdateMulti to achieve the equivalent of this example ? (from SQL Server 2000 Books Online):

-- This example updates the state column for the first 10 authors from the authors table. UPDATE authors SET state = 'ZZ' FROM (SELECT TOP 10 * FROM authors ORDER BY au_lname) AS t1 WHERE authors.au_id = t1.au_id

This thread http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=590&HighLight=1 suggests there isn't, but we've had a new release since then ... simple_smile

Thanks in advance Mike

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 09-Oct-2007 07:28:43   

Hi Mike, there's not a direct way to do that. However, you can workaround this if you rewrite the SQL:

UPDATE authors
SET state = 'ZZ'
FROM authors
WHERE authors.au_id IN
  (SELECT DISTINCT TOP 2  authors.au_id
  FROM authors 
  ORDER BY au_lname)

So, at code you would use a FieldCompareSetPredicate. Something like:

AuthorEntity authorNewValues = new AuthorEntity();
orderNewValues.State = "ZZ";

ISortExpression sorter = new SortExpression();
sorter.Add(new SortClause(AuthorFields.AuLname, null, SortOperator.Ascending));

FieldCompareSetPredicate inSetFilter = new FieldCompareSetPredicate(AuthorFields.AuId, null, AuthorFields.AuId, null, SetOperator.In, null, null, "", 10, sorter);

IRelationPredicateBucket filter = new RelationPredicateBucket();
filter.PredicateExpression.Add(inSetFilter);

using (DataAccessAdapter adapter = new DataAccessAdapter())
{
    adapter.UpdateEntitiesDirectly(orderNewValues, filter);

}

Regards

David Elizondo | LLBLGen Support Team
slade52
User
Posts: 46
Joined: 15-Aug-2007
# Posted on: 09-Oct-2007 23:45:04   

Thankyou David.
I'll give that a go.

Regards Mike

slade52
User
Posts: 46
Joined: 15-Aug-2007
# Posted on: 12-Oct-2007 00:20:42   

In case anyone else wants to know, here is the code I am using. The problem was I needed to get X records from the "DebtorBalanceArchive" table that have a NULL "ExtractBatchId" and set the ExtractBacthId for those records to a supplied value. Here is the code (comments welcome if I've messed it up simple_smile )

using SelfServicing


DebtorBalanceArchiveCollection balances = new DebtorBalanceArchiveCollection();

IPredicateExpression updateFilter = new PredicateExpression();
updateFilter.Add(new FieldCompareSetPredicate(
    DebtorBalanceArchiveFields.DebtorBalanceUid,
    DebtorBalanceArchiveFields.DebtorBalanceUid,
    SetOperator.In,
    (new FieldCompareNullPredicate(DebtorBalanceArchiveFields.ExtractBatchId)), 
    null,
    string.Empty,
    NumberOfRecords,
    null,
    false,
    null));

DebtorBalanceArchiveEntity balanceUpdateEntity = new DebtorBalanceArchiveEntity();
balanceUpdateEntity.ExtractBatchId = batchEntity.ExtractBatchId;

tran.Add(balances);
balances.UpdateMulti(balanceUpdateEntity, updateFilter);

DebtorBalanceUid is the table's PK.

Thanks again for your help David. Cheers

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 12-Oct-2007 00:41:21   

Your code looks good so far. Thanks for the feedback.

Thanks again for your help David.

Always is a pleasure to be helpful wink

David Elizondo | LLBLGen Support Team
Posts: 14
Joined: 12-Dec-2005
# Posted on: 30-Jan-2008 20:45:51   

Thank you both - You saved me at least a couple of hours! This was exactly what I needed.