Question : Wildcard for Integer MS SQL 2008

Hello Experts,

I'm trying to build a recordset query, that returns results based on a location selected.
The location value sent to the page that processes the query is an integer, for instance -

1 = Portsmouth
2 = Southampton
3 = London
4 - Birmingham
etc

This works fine if a variable ios sent, however the form can be submitted without a location being selected, in which case it should return results for all locations

If it were a string that was being sent, I could use the % wildcard, however I can't find a way of doing this.

I'm building the recordset using dreamweaver, which demands a default value.

My recordset currently looks like the code in the code section -

FYI 'MMColParam' references the variable && it is the line begining 'AND AD.JBALocation IN(' that i am strugling with

Any help you can offer will be gratefully appreciated -

Thank you
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
SELECT AD.JBAID, 
Lower(AD.JBATitle) AS JBATitle, 
AD.JBALocation, 
AD.JBACategory 
FROM dbo.JBAdvert AD 
WHERE JBASiteID = 30 

AND AD.JBALocation IN(Select JBLocation from JBLocation where JBLID = COALESCE('MMColParam', JBLID)) 

ORDER BY AD.JBAID DESC

Answer : Wildcard for Integer MS SQL 2008

SQL Server normally produce generalized plans that tries to work best in all situations.
You probably innately know that when @JBLID is non-zero, it will be a lot faster to start the multi-table query from JBAdvert filtered by JBALocation, but SQL Server needs the plan (which it will reuse) to work well "in most/average cases".
Bearing that in mind, here is one way to produce different plans from the same SP using dynamic SQL.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
CREATE PROCEDURE [dbo].[1122]
@SiteID int,
@JBLID int,
@JBCID int,
@Keywords nvarchar(50)
AS
declare @nsql nvarchar(max);
set @nsql = '
Select
AD.JBAID,          
Lower(AD.JBATitle) AS JBATitle,          
AD.JBALocation,          
AD.JBACategory,          
AD.JBAPayRate,          
CONVERT(CHAR(11),JBADatePosted,106) AS JBADatePosted,          
Lower(left(AD.JBADescription, 300) + ''...'') as JBADescription,  
AD.JBAFeaturedJob,  
AD.JBAOverWrite,  
CL.JBCLID,  
CL.JBCLName,          
CL.JBCLLogo,          
AV.ADViews,          
AP.Applications    
FROM dbo.JBAdvert AD          
left join (SELECT AP.JBAPAdvertID, COUNT(AP.JBAPID) Applications FROM dbo.JBApplication AP GROUP BY AP.JBAPAdvertID) AP on AP.JBAPAdvertID = AD.JBAID          
left join (SELECT AV.AdvertID, AV.AdViews ADViews FROM dbo.JBADView AV GROUP BY AV.AdvertID, AV.AdViews) AV on AV.AdvertID = AD.JBAID          
inner join dbo.JBClient CL on CL.JBCLID = AD.JBAClientID 
WHERE JBASiteID = @SiteID 
AND AD.JBALocation IN (Select JBLocation from JBLocation ' +
	case when @JBLID = '0' then '' else
	'where (JBLID = ' + str(@JBLID) + ')' end +
')
AND AD.JBACategory IN(Select JBCategoryLabel from JBCategories ' +
	case when @JBCID = '0' then '' else
	'where (JBCID = ' + str(@JBCID) + ')' end +
') ' +
	case when @Keywords = '' then '' else
	'AND JBADescription LIKE ''%''+ @Keywords + ''%'' ' end +
'order by JBAID desc';

exec sp_executeSQL @nsql, N'@Keywords nvarchar(50)', @Keywords
GO
Random Solutions  
 
programming4us programming4us