English 中文(简体)
What s the best method to do paging in my ASP page
原标题:

What s the best method to do paging in my ASP page when displaying a list of items?

I knew that there is no equivalent to MySQL s LIMIT clause present in SQL Server, either 2000 or 2005.

How can I retrieve some particular records (Records 20 to 30) from the DB?

Do i need to fetch all the records from the database and do something in my ASP page to display the required records?

最佳回答

Whats the best method to do paging in my ASP page when displaying a list of items ?

I just want to add one more feature to Mr. Wheat s answer. Why not u are trying to use the Take () and Skip() feature of linq(obviously if u are using dotnet framework 3.5+)

It is indeed helpful while working with large datasets.

Have a look Using Take and Skip method in LINQ queries

I knew that there is no MySQL LIMIT clause present in SQL server(both 2000 and 2005 should support).how can i retrieve some particular records (Record 20 -30) from DB ?

You can do this in SQLSERVER 2005+ by using ranking function Row_Number() among other alternatives. A sample example is included herewith

First I am creating a dummy table and inserting some 50 records

declare @tbl table(name varchar(50),age int)
;with num_cte as
(   select 1 as rn
    union all
    select rn+1 from num_cte where rn<50
)  
insert @tbl
select  names ,rn + 20 ageval                
from num_cte
cross apply( select   name  + CAST(rn as varchar(2))  AS names) names
select * from @tbl

Now by using the Row_Number() function I am picking up records between 20 & 30

select name,age from(
select ROW_NUMBER()over (order by age) as rownum,name,age from @tbl) X
where X.rownum between 20 and 30

However, for achieving the same in SQL SERVER 2000 the below query will help

select name,age from(
select t1.name,t1.age,
(select count(*)+1 from @tbl where name<>t1.name and age<=t1.age) rownum
from @tbl t1
)X(name,age,rownum)
where rownum between 20 and 30
问题回答

Youd need to use ROW_NUMBER (SQL Server 2005+)

 SELECT * FROM
    (SELECT a.*, ROW_NUMBER() OVER (ORDER BY hire_date) rn
    FROM hr.employees AS OF TIMESTAMP (TIMESTAMP  2009-01-29 10:30:00 ) a)
 WHERE rn BETWEEN 10 AND 19

Related answer

Using ROW_NUMBER, you are numering and sorting the inherently unsorted group (the table). Once you have an ordered set instead of just a set, you can now the sentence "I want all the rows from 10 to 19" makes sense.

You will have to use ASP code to keep both the upper and lower elements, so you can ask for the next or previous subset of rows to show.





相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签