?
Solved

Dynamic WHERE clause with paging functionality in SQL 2005

Posted on 2008-06-24
5
Medium Priority
?
365 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:Deepika_Rastogi
ID: 21863126
check whether the ' character is passed properly in the parameter
0
 

Author Comment

by:andysross
ID: 21869776
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 500 total points
ID: 21872914
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
ID: 21872921
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
ID: 31470462
This solution was great. The only thing I had to do was cast the int's to varchar within the string
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 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