Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Get RowCount from WITH Statement for 2 Select

Author  Topic 

prakashdotc
Starting Member

25 Posts

Posted - 2009-06-10 : 00:36:41
Hi All,

I have a requirement, in which i have to get list of Item's based on some search criteria.

As this requirement is based on paging i'm using Row_Number() method along with 'WITH' and here is my SP


CREATE PROCEDURE Items_Search
@PageSize INT,
@PageIndex INT,
@ID INT,
@ItemCode VARCHAR(18),
@ItemDesc VARCHAR(80),
@ItemCategory TINYINT,
@Total INT OUTPUT
AS
BEGIN

WITH ITEMSEARCH AS
(
SELECT ID, ItemCode, ItemDesc, ItemCategory, Image150x150, Image250x250, Image500x500,
ROW_NUMBER() OVER (ORDER BY ID) AS RowNumber
FROM ITEMS
WHERE (...)
)

SELECT * FROM ITEMSEARCH WHERE (RowNumber > ((@PageSize * @PageIndex) - @PageSize) AND RowNumber <= (@PageSize *
@PageIndex))


END

Here my Requirement is to Get the Total Row Count which is got as a result of implementing 'WHERE' condition,

the where condition tend to grow as more when more search criteria is added, in that case executing select statement
with the WHERE condition for more than once to get the Total Count and RecordSet will not be good

And only one SELECT statement can follow a WITH Class.

So as per this condition I need help to

Execute the select statement with WHERE clause only once from that i need TOTAL as well as the
RecordSet of the SELECT Statement based on Row_Number() Condition.


Please raise question if i'm not clear with my problem, so that i'll try to explain it better

Thanks


nathans
Aged Yak Warrior

938 Posts

Posted - 2009-06-10 : 02:25:09
You can reference the cte again in your select like:

SELECT *, (select count(*) from ItemSearch)
FROM ITEMSEARCH WHERE (RowNumber > ((@PageSize * @PageIndex) - @PageSize) AND RowNumber <= (@PageSize * @PageIndex))



Am I understanding you correctly?



Nathan Skerl
Go to Top of Page

prakashdotc
Starting Member

25 Posts

Posted - 2009-06-10 : 03:37:40
Hi nathans,

Thank U for your solution, it's working fine.

I need another help.

In the Same SP which is specified in the previous post,
i'll pass 'SortBy' as another parameter in that i'll pass values as 0, 1, 2 etc

based on this SortBy Value i have to include ORDER BY to the 'SELECT' query which comes after the 'WITH' class

how to do that....?

Thanks in Advance

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-10 : 14:02:13
quote:
Originally posted by prakashdotc

Hi nathans,

Thank U for your solution, it's working fine.

I need another help.

In the Same SP which is specified in the previous post,
i'll pass 'SortBy' as another parameter in that i'll pass values as 0, 1, 2 etc

based on this SortBy Value i have to include ORDER BY to the 'SELECT' query which comes after the 'WITH' class

how to do that....?

Thanks in Advance




there's no use sorting records inside with... you should do it in last select as below

SELECT...
...
ORDER BY CASE @SortBy
WHEN 0 THEN firstfield
WHEN 1 THEN secondfield
WHEN 2 THEN thirdfield
END
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2009-06-10 : 14:28:25
I agree with Visakh, use his example to dynamically sort.

Go to Top of Page
   

- Advertisement -