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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 basic sql sorting and paging script need help!

Author  Topic 

ajcool123
Starting Member

8 Posts

Posted - 2008-10-27 : 14:31:01
Hi Guys,

Okay here is my basic script that is failing. I keep getting the syntax error...

Msg 102, Level 15, State 1, Procedure uspItem_RtrvItemForSKU, Line 47
Incorrect syntax near 'DESC'.

Thank you for the help!



SELECT Item.Id
FROM Item
WHERE
ROW_NUMBER() OVER
(
ORDER BY SKU
/* same expression as in the ORDER BY of the whole query */
)
BETWEEN (@Page - 1) * @PageSize + 1 AND (@Page * @PageSize)
/* AND more conditions ... */
ORDER BY
CASE WHEN @OrderByDirection = 'A' THEN
CASE @OrderBy
WHEN 'SKU' THEN Item.SKU
WHEN 'Description' THEN Item.Description
END
END,
CASE WHEN @OrderByDirection = 'D' THEN
CASE @OrderBy
WHEN 'SKU' THEN Item.SKU
WHEN 'Description' THEN Item.Description
END
END DESC

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-10-27 : 14:56:41
are you sure this is the correct syntax for ROW_NUMBER()?? You can only use row_number() inside order by or select
Go to Top of Page

ajcool123
Starting Member

8 Posts

Posted - 2008-10-27 : 15:06:53
Okay what about this script? It fails with the same syntax error...



WITH ItemList AS
(
SELECT ROW_NUMBER() OVER(ORDER BY Item.SKU) AS RowNum, Item.Id
FROM Item left join (SELECT Cost.Id
FROM Cost, VendorFactory
WHERE Cost.VendorFactoryId = VendorFactory.Id) cost
ON Item.CostId = cost.Id

WHERE (isnull(@SKU,'') = '' or (Item.SKU like @SKU))
and (isnull(@Description,'') = '' or (Item.Description like @Description))
and Item.StatusId in (SELECT ChangeType
FROM dbo.SplitChangeTypes(@StatusIdList))
)

SELECT *
FROM ItemList
WHERE RowNum BETWEEN (@Page - 1) * @PageSize + 1 AND @Page * @PageSize
ORDER BY
CASE WHEN @OrderByDirection = 'A' THEN
CASE @OrderBy
WHEN 'SKU' THEN Item.SKU
WHEN 'Description' THEN Item.Description
/* more... */
END
END,

CASE WHEN @OrderByDirection = 'D' THEN
CASE @OrderBy
WHEN 'SKU' THEN Item.SKU
WHEN 'Description' THEN Item.Description /* more... */
END
END DESC

Go to Top of Page

cr8nk
Yak Posting Veteran

77 Posts

Posted - 2008-10-27 : 15:37:11
You need to put your dynamic statement for ordering in your row_number. I like this approach better

;WITH ItemList AS
(
SELECT
CASE WHEN @OrderByDirection = 'A' AND @OrderBy = 'SKU' THEN
ROW_NUMBER() OVER(ORDER BY Item.SKU)
WHEN @OrderByDirection = 'A' AND @OrderBy = 'SKU' THEN
ROW_NUMBER() OVER(ORDER BY Item.Description)
END AS RowNum
, Item.Id
FROM Item left join (SELECT Cost.Id
FROM Cost, VendorFactory
WHERE Cost.VendorFactoryId = VendorFactory.Id) cost
ON Item.CostId = cost.Id

WHERE (isnull(@SKU,'') = '' or (Item.SKU like @SKU))
and (isnull(@Description,'') = '' or (Item.Description like @Description))
and Item.StatusId in (SELECT ChangeType
FROM dbo.SplitChangeTypes(@StatusIdList))
)

SELECT *
FROM ItemList
WHERE RowNum BETWEEN (@Page - 1) * @PageSize + 1 AND @Page * @PageSize
Go to Top of Page

ajcool123
Starting Member

8 Posts

Posted - 2008-10-27 : 19:42:11
Thank you for that. However I need to access a table in the subquery for my ORDERBY.

I need to beable to access VendorFactory.Factory in the ORDERBY. For example, Thank you!


WHEN @OrderByDirection = 'A' AND @OrderBy = 'Vendor' THEN
ROW_NUMBER() OVER(ORDER BY VendorFactory.Vendor)
Go to Top of Page

cr8nk
Yak Posting Veteran

77 Posts

Posted - 2008-10-27 : 20:46:58
Hi,

You just need to keep adding columns to the select list and order by conditions to case statement in the CTE.

[CODE]
;WITH ItemList AS
(
SELECT
CASE WHEN @OrderByDirection = 'A' AND @OrderBy = 'SKU' THEN ROW_NUMBER() OVER(ORDER BY Item.SKU)
WHEN @OrderByDirection = 'A' AND @OrderBy = 'SKU' THEN ROW_NUMBER() OVER(ORDER BY Item.Description)
-- Add more order by conditions here if needed
END AS RowNum
, Item.Id
, VendorFactory.Id
-- add more fields here if needed
FROM Item
LEFT JOIN (SELECT Cost.Id
FROM Cost, VendorFactory
WHERE Cost.VendorFactoryId = VendorFactory.Id) cost
ON Item.CostId = cost.Id

WHERE (isnull(@SKU,'') = '' or (Item.SKU like @SKU))
AND (isnull(@Description,'') = '' or (Item.Description like @Description))
AND Item.StatusId in (SELECT ChangeType
FROM dbo.SplitChangeTypes(@StatusIdList))
)

SELECT *
FROM ItemList
WHERE RowNum BETWEEN (@Page - 1) * @PageSize + 1 AND @Page * @PageSize
[/CODE]

Does that make sense?
Go to Top of Page

ajcool123
Starting Member

8 Posts

Posted - 2008-10-27 : 21:18:17
yeah that does but i keep getting this error.
thoughts?


Msg 4104, Level 16, State 1, Procedure uspItem_RtrvItemForSKU, Line 25
The multi-part identifier "VendorFactory.Id" could not be bound.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-27 : 23:55:52
quote:
Originally posted by ajcool123

yeah that does but i keep getting this error.
thoughts?


Msg 4104, Level 16, State 1, Procedure uspItem_RtrvItemForSKU, Line 25
The multi-part identifier "VendorFactory.Id" could not be bound.


should be modified as below

;WITH ItemList AS
(
SELECT
CASE WHEN @OrderByDirection = 'A' AND @OrderBy = 'SKU' THEN ROW_NUMBER() OVER(ORDER BY Item.SKU)
WHEN @OrderByDirection = 'A' AND @OrderBy = 'SKU' THEN ROW_NUMBER() OVER(ORDER BY Item.Description)
-- Add more order by conditions here if needed
END AS RowNum
, Item.Id
, costVendorFactory.Id
-- add more fields here if needed
FROM Item
LEFT JOIN (SELECT Cost.Id
FROM Cost, VendorFactory
WHERE Cost.VendorFactoryId = VendorFactory.Id) cost
ON Item.CostId = cost.Id

WHERE (isnull(@SKU,'') = '' or (Item.SKU like @SKU))
AND (isnull(@Description,'') = '' or (Item.Description like @Description))
AND Item.StatusId in (SELECT ChangeType
FROM dbo.SplitChangeTypes(@StatusIdList))
)

SELECT *
FROM ItemList
WHERE RowNum BETWEEN (@Page - 1) * @PageSize + 1 AND @Page * @PageSize
Go to Top of Page
   

- Advertisement -