Solved

Dynamic WHERE clause with paging functionality in SQL 2005

Posted on 2008-06-24
5
354 Views
Last Modified: 2010-04-21
Hi

I would like to pass a where clause as a parameter (e.g.: FirstName Like '%Jo%')

When I run this stored procedure in query analyzer I get:
Incorrect syntax near ')'.
Invalid object name 'ItemEntries'.

I followed an example on a previous question on expert exchange but the solution did not seem to work for me.

Seems to be the dynamic WITH  that throws an error.

Is it in anyway possible to resolve this problem?
ALTER PROCEDURE [dbo].[PagedUserList]

(

	@PageIndex int,

	@NumRows int,

	@SearchText nvarchar(1000)

)

AS
 

BEGIN
 

	DECLARE @nSQL nvarchar(1090)

	SET @nSQL = 'SELECT (SELECT COUNT(*) FROM Users WHERE '+@SearchText+') AS MemberCount'

	EXECUTE sp_executeSQL @nSQL
 

	

	DECLARE @startRowIndex int;

	SET @startRowIndex = (@PageIndex * @NumRows) + 1;
 

	DECLARE @wSQL nvarchar(1090)

	SET @wSQL = 'WITH ItemEntries AS (SELECT ROW_NUMBER() OVER (ORDER BY LastName DESC) AS Row, UserID, FirstName, LastName FROM Users WHERE '+@SearchText+')'

	EXEC sp_executesql @wSQL

	

	SELECT UserID, FirstName, LastName

	FROM ItemEntries

	WHERE Row between 

	@startRowIndex and @StartRowIndex+@NumRows-1

	

END

Open in new window

0
Comment
Question by:andysross
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:Deepika_Rastogi
Comment Utility
check whether the ' character is passed properly in the parameter
0
 

Author Comment

by:andysross
Comment Utility
Hi

I made the parameter @SearchText redundent to eliminate how I may be passing this parameter and ran it again in query analyzer with the result below. I still get the same error


---------------------------
PagedUserList 1, 9
---------------------------
(1 row(s) affected)
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
Msg 208, Level 16, State 1, Procedure PagedUserList, Line 24
Invalid object name 'ItemEntries'.
set ANSI_NULLS ON

set QUOTED_IDENTIFIER ON

go
 

ALTER PROCEDURE [dbo].[PagedUserList]

(

	@PageIndex int,

	@NumRows int

	--@SearchText nvarchar(1000)

)

AS

 

BEGIN

 

	DECLARE @nSQL nvarchar(1090)

	SET @nSQL = 'SELECT (SELECT COUNT(*) FROM Users WHERE  Status = 1) AS MemberCount'

	EXECUTE sp_executeSQL @nSQL

 

	

	DECLARE @startRowIndex int;

	SET @startRowIndex = (@PageIndex * @NumRows) + 1;

 

	DECLARE @wSQL nvarchar(1090)

	SET @wSQL = 'WITH ItemEntries AS (SELECT ROW_NUMBER() OVER (ORDER BY LastName DESC) AS Row, UserID, FirstName, LastName FROM Users WHERE Status = 1)'

	EXEC sp_executesql @wSQL

	

	SELECT UserID, FirstName, LastName

	FROM ItemEntries

	WHERE Row between 

	@startRowIndex and @StartRowIndex+@NumRows-1

	

END

Open in new window

0
 
LVL 2

Accepted Solution

by:
Deepika_Rastogi earned 125 total points
Comment Utility
As u r using With clause the select query of the table with which

DECLARE @wSQL nvarchar(1090)
SET @wSQL = 'WITH ItemEntries AS (SELECT ROW_NUMBER() OVER (ORDER BY LastName DESC) AS Row, UserID, FirstName, LastName FROM Users WHERE Status = 1)
SELECT UserID, FirstName, LastName
FROM ItemEntries
WHERE Row between
@startRowIndex and @StartRowIndex+@NumRows-1 '

EXEC sp_executesql @wSQL
      
      



0
 
LVL 2

Expert Comment

by:Deepika_Rastogi
Comment Utility
Soory

As u r using With clause the select query of the table with which With clase is attached should be embed within the string itself
0
 

Author Closing Comment

by:andysross
Comment Utility
This solution was great. The only thing I had to do was cast the int's to varchar within the string
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Database tuning – How to start and what to tune. This question is frequently asked by many people, both online and offline. There is no hard and fast rule-of-thumb for performance tuning, however, before beginning the tuning process one should a…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now