syntax for a string in where clause of a dynamic t-sql query

Hi experts,

I'm using sql server 2008.

I'm using the Employees table from the Northwind sample database.

Query 1A

Filter by EmployeeID

I have a query that works fine, it looks like this:

DECLARE @EmployeeID INT
SET @EmployeeID = -3
SELECT [EmployeeID],[LastName],[FirstName] FROM [Northwind].[dbo].[Employees]
WHERE ( (@EmployeeID = -3) OR ([EmployeeID] = @EmployeeID) )

Open in new window


I'm using -3 to display all records.
When I set the @EmployeeID parameter to -3 the query returns all records like this:

p1.PNG
When I set the @EmployeeID parameter to 3 the query returns all records for EmployeeID of 3 like this:

p2.PNG
Query 1B

Filter by EmployeeID

I'm practicing writing dynamic sql queries.
So then I wrote this query. It works just fine. It gives the same results as Query 1A.

DECLARE @EmployeeID INT
SET @EmployeeID = -3

DECLARE @mSQL as varchar(5000)

---------------- Select ----------------

Select @mSQL = 'SELECT [EmployeeID],[LastName],[FirstName] FROM [Northwind].[dbo].[Employees]' 

Select @mSQL = @mSQL + ' Where ( ([EmployeeID] = ' + RTrim(@EmployeeID) + ') Or (' + RTrim(@EmployeeID) + ' =  -3) )' 

---------------- Select ----------------

Exec(@mSQL)

Open in new window



Query 2A

Filter By LastName

So now I want to do the same query as Query 1A but filter by LastName.
I wrote this query and it works fine.

DECLARE @LastName VARCHAR(30)
SET @LastName = 'ALL'
SELECT [EmployeeID],[LastName],[FirstName] FROM [Northwind].[dbo].[Employees]
WHERE ( (@LastName = 'ALL') OR ([LastName] = @LastName) )

Open in new window


I'm using the string 'ALL'  to display all records.
When I set the @LastName parameter to 'ALL' the query returns all records.
When I set the @LastName parameter to 'Leverling' the query returns all records with LastName 'Leverling.


Query 2B

Filter By LastName

So now I have the query that looks like this.
The results of this query should be the same as the results of Query 2A. But I'm getting an error.  
In my where clause where i set ALL i'm not sure of the syntax to use for a string.

DECLARE @LastName VARCHAR(30)
SET @LastName = 'ALL'

DECLARE @mSQL as varchar(5000)

---------------- Select ----------------

Select @mSQL = 'SELECT [EmployeeID],[LastName],[FirstName] FROM [Northwind].[dbo].[Employees]' 

Select @mSQL = @mSQL + ' Where ( ([LastName] = ' + RTrim(@LastName) + ') Or (' + RTrim(@LastName) + ' =  ALL) )' 

---------------- Select ----------------

Exec(@mSQL)

Open in new window


Anyone know how to fix my Query 2B? It should give the same results as Query 2A.
When I set the @LastName parameter to 'ALL' the query should return all records.
When I set the @LastName parameter to 'Leverling' the query should returns all records with LastName 'Leverling.
LVL 1
maqskywalkerAsked:
Who is Participating?
 
NorieVBA ExpertCommented:
I'm not 100% sure what you are trying to do or if this is the best way to do it but this will return a valid query.

---------------- Select ----------------

Select @mSQL = 'SELECT [EmployeeID],[LastName],[FirstName] FROM [Northwind].[dbo].[Employees]' 

Select @mSQL = @mSQL + ' Where ( ([LastName] = ''' + RTrim(@LastName) + ''') Or (''' +@LastName + '''=  ''ALL''))' 

---------------- Select ----------------

Open in new window

0
 
Jan LouwerensSoftware EngineerCommented:
Try using 2 single quotes around the string constant:
Select @mSQL = @mSQL + ' Where ( ([LastName] = ' + RTrim(@LastName) + ') Or (' + RTrim(@LastName) + ' =  ''ALL'') )' 

Open in new window

0
 
maqskywalkerAuthor Commented:
Thanks Norie. Your Syntax in your Where clause worked.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.