Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting row number from union join + paging

Status
Not open for further replies.

emozley

Technical User
Jan 14, 2003
769
GB
Hi,

I am trying to write a stored procedure that allows me to do a select union command and get a row number for each row and then use this recordset for paging.

The code I have so far is:

Code:
USE [Clients]
GO
/****** Object:  StoredProcedure [dbo].[PartySearch]    Script Date: 06/26/2012 10:08:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[PartySearch] 
@PageNum INT, @PageSize INT, @search varchar(128)

AS

WITH searchRN AS
(
SELECT *, ROW_NUMBER() OVER(ORDER BY Result) ROW_NUM
FROM
(
SELECT Users.FirstName + ' ' + Users.Surname AS Result
FROM Users
WHERE Users.FirstName LIKE '%'+@search+'%' OR Users.Surname LIKE '%'+@search+'%'

UNION

SELECT Organisation AS Result
FROM Users
WHERE Users.Organisation LIKE '%'+@search+'%' 

UNION

SELECT 'Held For: '+HeldFor AS Result
FROM Schedules
WHERE Schedules.HeldFor LIKE '%'+@search+'%'

UNION

SELECT 'Relating to: '+RelatingTo AS Result
FROM Schedules
WHERE Schedules.RelatingTo LIKE '%'+@search+'%'
)
)    <------ error is here apparently

SELECT * 
FROM searchRN
WHERE RowNum BETWEEN (@PageNum - 1) * @PageSize + 1 
                  AND @PageNum * @PageSize

However it says there is an error at the final bracket. Any ideas why it is failing?

Thanks very much

Ed
 
Ed,
I think you need a reference on your sub-query; try adding in one at the end.

Code:
...
)[red][b]T1[/b][/red]
)    

SELECT * 
FROM searchRN
WHERE RowNum BETWEEN (@PageNum - 1) * @PageSize + 1 
                  AND @PageNum * @PageSize

soi là, soi carré
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top