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
 General SQL Server Forums
 New to SQL Server Programming
 select multiple id's

Author  Topic 

duncant
Starting Member

18 Posts

Posted - 2010-11-15 : 12:15:55
select ID, eMail from tbl_users
where (ID IN (@EmailOne,@EmailTwo,@EmailThree))

Problem is that sometimes there is only going to be 1 email, maybe 2 and sometimes 3.
At the moment 3 is okay, 1 or 2 breaks the sql code, how would I account for 1 or 2 emails?

Sachin.Nand

2937 Posts

Posted - 2010-11-16 : 01:27:09
Doesn't quite make sense of what you are trying to say.

declare @t table(id int)
insert @t
select 1 union all
select 4 union all
select 7
select * from @t

declare @id1 int=1
declare @id2 int
declare @id3 int=7

select * from @t
where id in(@id1,@id2,@id3)


PBUH

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-11-16 : 03:12:56
<<
1 or 2 breaks the sql code,
>>

What did you mean by it?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

duncant
Starting Member

18 Posts

Posted - 2010-11-16 : 03:19:29
Ok, it made sense in my head.

By 1 or 2, I mean the email parameters, i.e @EmailOne or @EmailTwo or @EmailThree.

If there is only 1 or 2 parameters being passed to the sql code, how could I account for this?

I appologise if I'm not making myself clear.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-11-16 : 04:39:13
Put NULL as default value to the variables

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

duncant
Starting Member

18 Posts

Posted - 2010-11-16 : 05:24:09
Thanks, didn't know it was going to be that easy!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-11-16 : 09:12:29
quote:
Originally posted by duncant

Thanks, didn't know it was going to be that easy!


No problem

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -