## 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
Solved

# Dynamic WHERE clause in Stored Procedure

Posted on 2011-02-17
314 Views
I have a stored procedure usp_X.

I am executing another stored procedure usp_Y inside usp_X with an output string(sWHERE) as below

(Desc1 = ’08-100’ AND Year=’2008’ AND ‘Desc2’ = ‘PERM-1’) OR
(Desc1 = ’09-110’ AND Year=’2009’ AND ‘Desc2’ = ‘PERM-2’) OR
(Desc1 = ’10-289’ AND Year=’2010’ AND ‘Desc2’ = ‘PERM-3’)

I want use the above ouput string in the WHERE clause as

SELECT
col1,
col2
FROM
table1 A join table2 B ON A.Id = B.Id
WHERE
--- use the above output here--- (like sWHERE)

How can i accomplish? would someone help me out !

Thanks.

0
Question by:cimscims
• 4
• 3
• 2
• +3

LVL 75

Expert Comment

ID: 34917448
EXEC( 'SELECT
col1,
col2
FROM
table1 A join table2 B ON A.Id = B.Id
WHERE 1=1 '+ sWhere )
0

LVL 143

Expert Comment

ID: 34917496
you could do that only by making your query in usp_X also dynamic.

``````declare @sql varchar(max)
declare @where varchar(max)

exec usp_Y @where output

set @sql = 'SELECT
col1,
col2
FROM
table1 A join table2 B ON A.Id = B.Id
WHERE ' + @where

exec(@sql)
``````
0

LVL 10

Expert Comment

ID: 34917499
EXEC( 'SELECT
col1,
col2
FROM
table1 A join table2 B ON A.Id = B.Id
WHERE 1=1 AND ( ' + sWhere + ')' )
0

LVL 51

Expert Comment

ID: 34917612
WHERE 1=1 AND ( ' + sWhere + ')' )

"1=1" is required in case sWhere is null
but when it is null 34917499 will fail
also 34917496 will fail
34917448 will also fail :)

so, we need to check for null/empty:

declare @sql varchar(max) = 'SELECT col1, col2 FROM table1 A join table2 B ON A.Id = B.Id';
if sWhere<>'' set @sql=@sql + ' WHERE ' + sWhere;
EXEDC (@sql)
0

LVL 51

Expert Comment

ID: 34917626

declare @sql varchar(max) = 'SELECT col1, col2 FROM table1 A join table2 B ON A.Id = B.Id';
if sWhere<>'' set @sql=@sql + ' WHERE ' + sWhere;
EXEC (@sql)
0

Author Comment

ID: 34917676
I am getting a synrtax error in the SELECT near the column
CONVERT(varchar,date,101) as date.
0

LVL 40

Expert Comment

ID: 34917748
You should enclose the date in quotes. Post your query.
0

Author Comment

ID: 34917809

EXEC( 'SELECT
Id,
desc1,
CONVERT(varchar,startdate,101) as 'start date',
CONVERT(varchar,enddate,101) as 'start date'
FROM
table1 A join table2 B ON A.Id = B.Id
WHERE 1=1 '+ sWhere )

0

LVL 51

Expert Comment

ID: 34917813
i dont see anything like "CONVERT(varchar,date,101) as date" above!
0

LVL 40

Accepted Solution

Sharath earned 250 total points
ID: 34917905
I think the 2nd date is end date. check this.
``````EXEC( 'SELECT
Id,
desc1,
CONVERT(varchar,startdate,101) as [start date],
CONVERT(varchar,enddate,101) as [end date]
FROM
table1 A join table2 B ON A.Id = B.Id
WHERE 1=1 '+ @sWhere )
``````
0

LVL 51

Assisted Solution

HainKurt earned 250 total points
ID: 34917983
@sWhere does not have and in front so, this should work...
``````EXEC( 'SELECT
Id,
desc1,
CONVERT(varchar,startdate,101) as [start date],
CONVERT(varchar,enddate,101) as [end date]
FROM
table1 A join table2 B ON A.Id = B.Id
WHERE '+ @sWhere )
``````
0

Author Closing Comment

ID: 34918026
Thanks. It worked.
0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be \$37.1B.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.