Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1)I want to fetch record of row num 3,How ?

2)How can i count the records ?except count(*).
Posted

You don't mention what database this is.

1) There are various tricks you can do with 'Row Number'. This example is for SQL 2008

SQL
WITH OrderedOrders AS
(
    SELECT SalesOrderID, OrderDate,
    ROW_NUMBER() OVER (ORDER BY OrderDate) AS 'RowNumber'
    FROM Sales.SalesOrderHeader
)
SELECT *
FROM OrderedOrders
WHERE RowNumber BETWEEN 50 AND 60;


http://msdn.microsoft.com/en-us/library/ms186734.aspx[^]

So, you could create some SQL for your table and show WHERE RowNumber = 3


2) Why you wouldn't want to use the function that is specifically designed for this purpose is beyond me - must be a homework task!

As the other answers have suggested, you'll need to iterate the rows and count them yourself.

If you need to do this purely in SQL (and again, I'm assuming SQL Server) then you could open a cursor and loop through the rows, incrementing a variable and returning the result.
 
Share this answer
 
Comments
Hiren solanki 24-Dec-10 6:56am    
Good answer !!
Manfred Rudolf Bihy 24-Dec-10 17:42pm    
Good one! 5+
Add the keyword SQL_CALC_FOUND_ROWS right after the keyword SELECT :

SELECT SQL_CALC_FOUND_ROWS t3.id, a,bunch,of,other,stuff FROM t1, t2, t3 WHERE (associate t1,t2, and t3 with each other) GROUP BY t3.id LIMIT 10,20

After that, run another query with the function FOUND_ROWS() :

SELECT FOUND_ROWS(); It should return the number of rows without the LIMIT clause.
 
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