| Author |
Topic |
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-05-20 : 22:40:46
|
| Hi All, I am trying to use Coalesce (in case @str is null) and inline_split_me ( because @str is an Array)declare @str varchar(100), @city varchar(100)SET @str= '121707,121708,121709'SET @city= 'Tokyo'CREATE FUNCTION dbo.ShowRecords (@str, @city)select etsid from eTicketMain where city=@city and etsid in (select * from inline_split_me(coalesce( @str, etsid )))The purpose of having Coalesce is that If @str=Null, all the records that is city=@city will show. If @str is not Null, then only the selected records ( records that are in etSID and city=@city) will show.Please advise.Thanks**Jonathan** |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-05-20 : 22:49:43
|
try thisselect t.etsidfrom eTicketMain t inner join inline_split_me(@str) con t.etsid = c.stror @str is nullwhere t.city = @city KH |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-05-21 : 00:36:29
|
| Also make sure if ShowRecords is SP or FunctionMadhivananFailing to plan is Planning to fail |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-05-21 : 01:47:16
|
quote: Originally posted by khtan try thisselect t.etsidfrom eTicketMain t inner join inline_split_me(@str) con t.etsid = c.stror @str is nullwhere t.city = @city KH
If I would like to add more condtions at the where clause, how should I do it?**Jonathan** |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-05-21 : 01:52:15
|
add in the WHERE clauseselect t.etsidfrom eTicketMain t inner join inline_split_me(@str) con t.etsid = c.stror @str is nullwhere t.city = @cityand <more condition> KH |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-05-22 : 01:58:09
|
quote: Originally posted by khtan try thisselect t.etsidfrom eTicketMain t inner join inline_split_me(@str) con t.etsid = c.stror @str is nullwhere t.city = @city KH
hmm....I tried that. It seems that when @str is null then no result is displayed because of the or@str is Null . That is not the desired outcome. The desired outcome should be all the result should be displayed with t.city = @city and etsid = any valueplease advise**Jonathan** |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-05-22 : 02:35:13
|
post your table DDL, some sample data and the desire result KH |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-05-22 : 04:59:25
|
| declare @str varchar(100)SET @str= '121707,121708,121709' <---case 1SET @str= Null < ---- case 2:disable this one if you want case 1declare @city varchar(100)SET @city= 'TPE'select t.etsidfrom eTicketMain t inner join inline_split_me(@str) con t.etsid = c.valueor @str is nullwhere t.eTDeparture = @cityDataetsid eTDeparture121701 KKD121703 TPE121704 TPE121705 TPE121706 TPE121707 TPE121708 TPE121709 TPEcase1 : Desired resultetsid121707121708121709case2 : Desired result121703121704121705121706121707121708121709**Jonathan** |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-05-22 : 05:29:50
|
| try this..select t.etsidfrom eTicketMain t where t.eTDeparture = @city and( ',' + @str + ',' like '%,' +t.etsid +',%' or @str is null) |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-05-22 : 05:55:24
|
| hmm....what if in the future the etSID number increases and it may be a problem by using like statement?**Jonathan** |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-05-22 : 06:05:57
|
| may i know what ur doing inside the inline udf.. |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
Posted - 2007-05-22 : 06:34:44
|
| I modify my code slightly and i got the solutionselect etsid from eTicketMain where city=@city and etsid in (select * from inline_split_me(@str)) or <---condition 1etsid=coalesce( @str, etsid ) <----condition 2so if @str is an array, condition 1 will be true and condition 2 will get not resultif @str is sinlge number such as 121707, condition 1,2 will get the resultit @str is null then condition 1 will get no result and condition2 will be etsid=etsid**Jonathan** |
 |
|
|
wangyc77
Yak Posting Veteran
65 Posts |
|
|
|