Solved

SQL query

Posted on 2009-05-08
3
216 Views
Last Modified: 2012-08-13
Experts,
I have pasted a piece from my stored procedure here. When @ssid id is not null but @RegionID is null there are values in DB that should be returned but this query does not. And same way if there are results for @RegionID is not null then there are results in Db but this query does not return those. I want it to work in any situation whether all the three variables are null or not it should display proper results. Can that be achieved ..?
declare @ssid varchar(max)

declare @stid varchar(max) 

declare @RegionID varchar(10) 
 

set @ssid='acd5eb63-6cf5-4ae0-9d26-afa76f2cd83f'

set @stid=''

set @RegionID=''
 
 

select distinct  cstitle as PageTitle,substring(convert(varchar(4000),csChallenge),0,150)+'...' as Excerpt,'Client Success' as AreaName,'' 

as OfficeName,convert(varchar(38),tbl_CS_Master.csID) as csid                               

,'' as ksID                

,'' as "Filename"            

,'' as ks_FName ,'' as ks_LName                      

from tbl_CS_Master left  join tbl_cs_service on tbl_cs_service.csid=                                  

tbl_CS_Master.csid left  join  tbl_CS_Regions on tbl_CS_Master.csID = tbl_CS_Regions.csID where 

(convert(varchar(36),tbl_cs_service.ssid) in                                   

(select cast(value as uniqueidentifier) from dbo.ParmsToList(@ssid)) or @ssid is null) 

and (tbl_CS_Regions.RegionID in (select cast( value as int) from dbo.ParmsToList(@RegionID)) or @RegionID is null)

Open in new window

0
Comment
Question by:dotnetpro
  • 2
3 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24340597
try this

select distinct
        cstitle as PageTitle,
        substring(convert(varchar(4000), csChallenge), 0, 150) + '...' as Excerpt,
        'Client Success' as AreaName, '' as OfficeName,
        convert(varchar(38), tbl_CS_Master.csID) as csid, '' as ksID,
        '' as "Filename", '' as ks_FName, '' as ks_LName
from    tbl_CS_Master
left  join tbl_cs_service on tbl_cs_service.csid = tbl_CS_Master.csid
left  join tbl_CS_Regions on tbl_CS_Master.csID = tbl_CS_Regions.csID
where   (  @ssid is null or convert(varchar(36), tbl_cs_service.ssid) in
         (
          select    cast(value as uniqueidentifier)
          from      dbo.ParmsToList(@ssid) )
           )
        and ( @RegionID is null OR    tbl_CS_Regions.RegionID in ( select   cast(value as int)
                                           from     dbo.ParmsToList(@RegionID) )
                )
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 24340601
I believe the conditions are read in order so try like this:
declare @ssid varchar(max)

declare @stid varchar(max) 

declare @RegionID varchar(10) 

 

set @ssid='acd5eb63-6cf5-4ae0-9d26-afa76f2cd83f'

set @stid=''

set @RegionID=''

 

 

select distinct  cstitle as PageTitle,substring(convert(varchar(4000),csChallenge),0,150)+'...' as Excerpt,'Client Success' as AreaName,'' 

as OfficeName,convert(varchar(38),tbl_CS_Master.csID) as csid                               

,'' as ksID                

,'' as "Filename"            

,'' as ks_FName ,'' as ks_LName                      

from tbl_CS_Master left  join tbl_cs_service on tbl_cs_service.csid=                                  

tbl_CS_Master.csid left  join  tbl_CS_Regions on tbl_CS_Master.csID = tbl_CS_Regions.csID 

where (@ssid is null or convert(varchar(36),tbl_cs_service.ssid) in                                   

(select cast(value as uniqueidentifier) from dbo.ParmsToList(@ssid))) 

and (@RegionID is null or tbl_CS_Regions.RegionID in (select cast( value as int) from dbo.ParmsToList(@RegionID)))

Open in new window

0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 500 total points
ID: 24340611
I noticed that you set @RegionID = '' in the procedure; therefore, maybe your issue is not that it is NULL but that it is empty string.  Try this:
declare @ssid varchar(max)

declare @stid varchar(max) 

declare @RegionID varchar(10) 

 

set @ssid='acd5eb63-6cf5-4ae0-9d26-afa76f2cd83f'

set @stid=''

set @RegionID=''

 

 

select distinct  cstitle as PageTitle,substring(convert(varchar(4000),csChallenge),0,150)+'...' as Excerpt,'Client Success' as AreaName,'' 

as OfficeName,convert(varchar(38),tbl_CS_Master.csID) as csid                               

,'' as ksID                

,'' as "Filename"            

,'' as ks_FName ,'' as ks_LName                      

from tbl_CS_Master left  join tbl_cs_service on tbl_cs_service.csid=                                  

tbl_CS_Master.csid left  join  tbl_CS_Regions on tbl_CS_Master.csID = tbl_CS_Regions.csID 

where (ISNULL(@ssid, '') = '' or convert(varchar(36),tbl_cs_service.ssid) in                                   

(select cast(value as uniqueidentifier) from dbo.ParmsToList(@ssid))) 

and (ISNULL(@RegionID, '') = '' or tbl_CS_Regions.RegionID in (select cast( value as int) from dbo.ParmsToList(@RegionID)))

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

746 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

8 Experts available now in Live!

Get 1:1 Help Now