Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
1.17/5 (5 votes)
See more:
Dear Expert,

I need a MSSql query, to get the datas in a set.

Here is the below sample table.


ID  | Name     | Code
----------------------
1   | Emil     | AB
10  | Remu     | AD
15  | Martin   | AB
17  | Dil      | AC
31  | Nav      | AC
42  | Prv      | AB
43  | Pra      | AD
45  | Mob      | AB
57  | ILD      | AB


My sqlquery will execute in three times, Each time the query output below.

First Execution Outupt.

ID  | Name     | Code
----------------------
1   | Emil     | AB
10  | Remu     | AD
15  | Martin   | AB


Second Execution Outupt.

ID  | Name     | Code
----------------------
17  | Dil      | AC
31  | Nav      | AC
42  | Prv      | AB


Third Execution Outupt.

ID  | Name     | Code
----------------------
43  | Pra      | AD
45  | Mob      | AB
57  | ILD      | AB



Actually this is for like a paging .....

please help me

Thanks and Regards,
Dileep
Posted

Paging for what ? The ASP.NET controls do paging automatically and if you only have 9 records and 3 per page, that is good enough. Otherwise, you can use the TOP keyword to generate a given number of records, you can user order by to order by ascending and descending, to get records descending up to the first record, then 3 records ascending to get the three on your page. You will need to string mash SQL, you can't use a variable for a TOP value in T-SQL.
 
Share this answer
 
select top 3 *
From XX
order by ID

SQL
select top 3 *
From XX
Where ID > 15
order by ID


...
 
Share this answer
 
First Execution
SQL
declare @pageindex int;
set @pageindex=0
select top 3 * from [tablename]
where ID not in (select top (@pageindex*3) id from [tablename] order by id ) order by id


Second execution
SQL
declare @pageindex int;
set @pageindex=1
select top 3 * from [tablename]
where ID not in (select top (@pageindex*3) id from [tablename] order by id ) order by id


Third execution
SQL
declare @pageindex int;
set @pageindex=2
select top 3 * from [tablename]
where ID not in (select top (@pageindex*3) id from [tablename] order by id ) order by id
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900