Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 help needed in logical query

Author  Topic 

sqllover
Constraint Violating Yak Guru

338 Posts

Posted - 2013-07-11 : 07:48:53
Hi i am working in sqlserver 2008. My table column as follows

Announcementid,Message,companyid, clientid, logo.am creating procedure which has two input parameters @company,@client.
Sample data:

1,'show',1000, null
2,'meeting', 1000, 100
3,'tourism', 1001, 101
4,'package', 1001, null

Let us assume if I pass 1000 as company input parameter, 100 as client input parameter, my desired out put should be 1st two row in my sample data. Logic here will be I have match where company=@company and client=@client
Where company=@company and client is null. This can be achieved by writing seperate query and can be joined with union all. Is there any better way to do this other than union all. Please give me some sample query.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2013-07-11 : 07:57:21
do you mean this?
where companyid = @Company and isnull(clientid,@Client) = @Client


Too old to Rock'n'Roll too young to die.
Go to Top of Page

sqllover
Constraint Violating Yak Guru

338 Posts

Posted - 2013-07-11 : 08:06:51
quote:
Originally posted by webfred

do you mean this?
where companyid = @Company and isnull(clientid,@Client) = @Client


Too old to Rock'n'Roll too young to die.



Hello friend, thanks for the reply. If this query works then well and good. I have to try this scenario and postback with the result.
Go to Top of Page

sqllover
Constraint Violating Yak Guru

338 Posts

Posted - 2013-07-11 : 12:46:54
hi Webfred,

I did test the logic you have given and seems it fails in one scenario.

select COUNT(*) from Announcement where
companyid = 1000 and isnull(clientid,null) = null;

gives count as 0. but i do have data in my table. any other options please ?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-07-11 : 13:02:14
where companyid = @Company and (clientid = @Client or @Client is null)



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

sqllover
Constraint Violating Yak Guru

338 Posts

Posted - 2013-07-11 : 13:32:01
Hi peso,

You logic is perfect with small typo. here is the perfect one

where companyid = @Company and (clientid = @Client or clientid is null)

thanks for the help. worked fine
Go to Top of Page
   

- Advertisement -