Using variable condition on where statement in stored procedure

Please resolve the best way to make this work...

I know the below would never work, I've tried a hlaf dozen solutions including dynamic sql, but this is just the best way to describe what I'm trying to do.

If city not passed as null then I want the query to include it in the where statement.
 
CREATE PROCEDURE sp_CityStateSearch

@City  VarChar(50),
@State VarChar(2)

SELECT *
FROM TABLE
WHERE
State = @State

CASE WHEN @CIty IS NOT NULL THEN
AND City = @City
Else
lexoAsked:
Who is Participating?
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.

TextReportCommented:
You need to build your SQL as a VarChar variable and then

EXEC (@sql)

Cheers, Andrew
0
TextReportCommented:
This should work.
CREATE PROCEDURE sp_CityStateSearch
 
@City  VarChar(50),
@State VarChar(2)
 
DECLARE @SQL VARCHAR(8000)
SET @SQL = 'SELECT *
FROM TABLE
WHERE State = ' + CHAR(39) + @State + CHAR(39)
 
CASE WHEN @CIty IS NOT NULL THEN
SET @SQL = @SQL + ' AND City = ' + CHAR(39) + @City + CHAR(39)
END
 
EXEC (@SQL)

Open in new window

0
lexoAuthor Commented:
TextReport I get the following errors:
Msg 156, Level 15, State 1, Line 20
Incorrect syntax near the keyword 'CASE'.
Msg 156, Level 15, State 1, Line 24
Incorrect syntax near the keyword 'EXEC'.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

TextReportCommented:
Think I gopt the CASE statement wrong so replaced with IF and it checked out OK
CREATE PROCEDURE sp_CityStateSearch
( 
@City  VarChar(50),
@State VarChar(2)
)
 
AS
 
DECLARE @SQL VARCHAR(8000)
SET @SQL = 'SELECT *
FROM TABLE
WHERE State = ' + CHAR(39) + @State + CHAR(39)
 
IF ISNULL(@CIty,'') <> '' 
	SET @SQL = @SQL + ' AND City = ' + CHAR(39) + @City + CHAR(39)
 
 
EXEC (@SQL)

Open in new window

0
appariCommented:
try this

CREATE PROCEDURE sp_CityStateSearch

@City  VarChar(50),
@State VarChar(2)

SELECT *
FROM TABLE
WHERE
State = @State
and City =
CASE WHEN @CIty IS NOT NULL THEN
@City else City end
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
appariCommented:
or this
CREATE PROCEDURE sp_CityStateSearch

@City  VarChar(50),
@State VarChar(2)

SELECT *
FROM TABLE
WHERE
State = @State
and ( City = @City or @City is null)
0
lexoAuthor Commented:
Sorry Text report, your solution worked but I like aaparis better because it doesnt use dynamic SQL which can be a pain sometimes.  Thank you both, split points 60/40
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
Microsoft SQL Server

From novice to tech pro — start learning today.