if(OBJECT_ID('EgTb')is not null)drop table [EgTB];
GO
create table [EgTB](Name varchar(50),country varchar(50))
insert into EgTB values('aaa','sg');
insert into EgTB values('bbb','uk');
insert into EgTB values('ccc','us');
insert into EgTB values('ddd','au');
GO
select * from EgTB;
declare @cou varchar(50), @firstCou varchar(50), @secondCou varchar(50);
set @firstCou='sg';
set @secondCou='uk';
if(@cou is null)
begin
set @cou=@firstCou+','+@secondCou;
end
select * from EgTB where (@cou is null or country in (@cou));
GO
if(OBJECT_ID('EgTb')is not null)drop table [EgTB];
GO
The above code is the one I stimulated.
there is a variable @cou
.
That variable is the one using in where.
what I want is.. if @cou
is null, I want to use @firstCou
and @secondCou
.
Instead of doing where country in (@firstCou,@secondCou)
I want where country in (@cou)
with those 2 variable values in @cou
.
cos the real thing is not 2 extra variable.. I will retreieve several records and I won't know the fixed record.
That's why I want to use IN()
But my problem is when I add those 2 values into @cou
, the @cou
value becomes 'sg,uk'
instead of 'sg','uk'
.
it is expected but how can i make it so that it becomes 'sg','uk'