Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

Dynamic WHERE clause in Stored Procedure

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
cimscims
Asked:
cimscims
  • 4
  • 3
  • 2
  • +3
2 Solutions
 
Aneesh RetnakaranDatabase AdministratorCommented:
EXEC( 'SELECT
                col1,
                col2
FROM
                table1 A join table2 B ON A.Id = B.Id
WHERE 1=1 '+ sWhere )
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
John ClaesCommented:
EXEC( 'SELECT
                col1,
                col2
FROM
                table1 A join table2 B ON A.Id = B.Id
WHERE 1=1 AND ( ' + sWhere + ')' )
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
HainKurtSr. System AnalystCommented:
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
 
HainKurtSr. System AnalystCommented:
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
 
cimscimsAuthor Commented:
I am getting a synrtax error in the SELECT near the column
 CONVERT(varchar,date,101) as date.
0
 
SharathData EngineerCommented:
You should enclose the date in quotes. Post your query.
0
 
cimscimsAuthor Commented:

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
 
HainKurtSr. System AnalystCommented:
i dont see anything like "CONVERT(varchar,date,101) as date" above!
0
 
SharathData EngineerCommented:
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
 
HainKurtSr. System AnalystCommented:
@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
 
cimscimsAuthor Commented:
Thanks. It worked.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now