get ROW_NUMBER value

Posts   
 
    
Daniel Sobrang G avatar
Posts: 2
Joined: 28-Dec-2011
# Posted on: 28-Dec-2011 14:09:03   

Is it possible to get the ROW NUMBER of a query?

I see LLBL generates a paginated query like below. I like to include the "__rowcnt" field to my Entity object. I have no idea if there a way to achieve it.

WITH __actualSet AS ( SELECT *, ROW_NUMBER() OVER (ORDER BY CURRENT_TIMESTAMP) AS __rowcnt FROM (SELECT DISTINCT TOP 20 ... FROM ... WHERE ... ORDER BY ... ASC) AS _tmpSet)

  SELECT ...
  FROM __actualSet 
  WHERE [__rowcnt] > @__rownoStart AND [__rowcnt] <= @__rownoEnd 
  ORDER BY [__rowcnt] ASC
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 28-Dec-2011 15:22:19   

Why do you want to include __rowcnt to your entity? AFAIK, this value have no significance outside the context of the query.

Daniel Sobrang G avatar
Posts: 2
Joined: 28-Dec-2011
# Posted on: 29-Dec-2011 07:11:56   

Thanks Walaa for responding. One reason is I need to display the record number on the UI.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 29-Dec-2011 11:22:47   

Here is how you can do it from an old Helpdesk thread:

You should implement IExpression as in the following thread: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=3829

Then you should use a DynamicList to fetch the needed data. And use the SetExpression() method to set an instance of the IExpression class you have implemented to one of the ResultSet fields.

The following is the Expression implementation.

[Serializable] public class RowNumberExpression : Expression { public RowNumberExpression(IEntityField2 field): base(field) { }

    public override string ToQueryText(ref int uniqueMarker, bool inHavingClause)
    {
        return string.Format("ROW_NUMBER() OVER (order by {0})", base.ToQueryText(ref uniqueMarker, inHavingClause));
    }
}