Solved

Dynamic WHERE clause in Stored Procedure

Posted on 2011-02-17
12
311 Views
Last Modified: 2012-06-27
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
Comment
Question by:cimscims
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
Comment Utility
EXEC( 'SELECT
                col1,
                col2
FROM
                table1 A join table2 B ON A.Id = B.Id
WHERE 1=1 '+ sWhere )
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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)

Open in new window

0
 
LVL 10

Expert Comment

by:John Claes
Comment Utility
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

by:HainKurt
Comment Utility
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

by:HainKurt
Comment Utility
typo above, should read as:

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

by:cimscims
Comment Utility
I am getting a synrtax error in the SELECT near the column
 CONVERT(varchar,date,101) as date.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 40

Expert Comment

by:Sharath
Comment Utility
You should enclose the date in quotes. Post your query.
0
 

Author Comment

by:cimscims
Comment Utility

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

by:HainKurt
Comment Utility
i dont see anything like "CONVERT(varchar,date,101) as date" above!
0
 
LVL 40

Accepted Solution

by:
Sharath earned 250 total points
Comment Utility
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 )

Open in new window

0
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 250 total points
Comment Utility
@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 )

Open in new window

0
 

Author Closing Comment

by:cimscims
Comment Utility
Thanks. It worked.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now