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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
NorieAnalyst Assistant Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
maqskywalkerAuthor Commented:
Thanks Norie. Your Syntax in your Where clause worked.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.