SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 help needed in logical query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sqllover
Constraint Violating Yak Guru

India
303 Posts

Posted - 07/11/2013 :  07:48:53  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

Germany
8683 Posts

Posted - 07/11/2013 :  07:57:21  Show Profile  Visit webfred's Homepage  Reply with Quote
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

India
303 Posts

Posted - 07/11/2013 :  08:06:51  Show Profile  Reply with Quote
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

India
303 Posts

Posted - 07/11/2013 :  12:46:54  Show Profile  Reply with Quote
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

Sweden
29910 Posts

Posted - 07/11/2013 :  13:02:14  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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

India
303 Posts

Posted - 07/11/2013 :  13:32:01  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.17 seconds. Powered By: Snitz Forums 2000