declare @EcountrydbID int,@Keyword varchar(200),@EproductId int ,@PicPath varchar(300),
@OldCountryDBID int,@OldCountryChoosedProductsIds varchar(1000);
set @OldCountryChoosedProductsIds='';
set @EproductId=0;
set @OldCountryDBID=0;


declare mycursor cursor for
--香港--
select * from (select top 6 EcountrydbID,Keyword from EcountryRelatedKeyword
inner join Ehotsearchkeyword on EhotsearchkeywordID=Ehotsearchkeyword_ID
where EcountryDBID=90 order by EcountryRelatedKeyword.ProductCount desc,SortOrder) t1
union all
--印度--
select * from (select top 6 EcountrydbID,Keyword from EcountryRelatedKeyword
inner join Ehotsearchkeyword on EhotsearchkeywordID=Ehotsearchkeyword_ID
where EcountryDBID=99 order by EcountryRelatedKeyword.ProductCount desc,SortOrder) t2
union all
--意大利--
select * from (select top 6 EcountrydbID,Keyword from EcountryRelatedKeyword
inner join Ehotsearchkeyword on EhotsearchkeywordID=Ehotsearchkeyword_ID
where EcountryDBID=104 order by EcountryRelatedKeyword.ProductCount desc,SortOrder) t3
union all
--马来西亚--
select * from (select top 6 EcountrydbID,Keyword from EcountryRelatedKeyword
inner join Ehotsearchkeyword on EhotsearchkeywordID=Ehotsearchkeyword_ID
where EcountryDBID=149 order by EcountryRelatedKeyword.ProductCount desc,SortOrder) t4
union all
--巴基斯坦--
select * from (select top 6 EcountrydbID,Keyword from EcountryRelatedKeyword
inner join Ehotsearchkeyword on EhotsearchkeywordID=Ehotsearchkeyword_ID
where EcountryDBID=170 order by EcountryRelatedKeyword.ProductCount desc,SortOrder) t5
union all
--美国--
select * from (select top 6 EcountrydbID,Keyword from EcountryRelatedKeyword
inner join Ehotsearchkeyword on EhotsearchkeywordID=Ehotsearchkeyword_ID
where EcountryDBID=223 order by EcountryRelatedKeyword.ProductCount desc,SortOrder) t6
union all
--沙特阿拉伯--
select * from (select top 6 EcountrydbID,Keyword from EcountryRelatedKeyword
inner join Ehotsearchkeyword on EhotsearchkeywordID=Ehotsearchkeyword_ID
where EcountryDBID=184 order by EcountryRelatedKeyword.ProductCount desc,SortOrder) t7
union all
--新加坡--
select * from (select top 6 EcountrydbID,Keyword from EcountryRelatedKeyword
inner join Ehotsearchkeyword on EhotsearchkeywordID=Ehotsearchkeyword_ID
where EcountryDBID=189 order by EcountryRelatedKeyword.ProductCount desc,SortOrder) t8
union all
--韩国--
select * from (select top 6 EcountrydbID,Keyword from EcountryRelatedKeyword
inner join Ehotsearchkeyword on EhotsearchkeywordID=Ehotsearchkeyword_ID
where EcountryDBID=115 order by EcountryRelatedKeyword.ProductCount desc,SortOrder) t9
union all
--台湾--
select * from (select top 6 EcountrydbID,Keyword from EcountryRelatedKeyword
inner join Ehotsearchkeyword on EhotsearchkeywordID=Ehotsearchkeyword_ID
where EcountryDBID=217 order by EcountryRelatedKeyword.ProductCount desc,SortOrder) t10
union all
--泰国--
select * from (select top 6 EcountrydbID,Keyword from EcountryRelatedKeyword
inner join Ehotsearchkeyword on EhotsearchkeywordID=Ehotsearchkeyword_ID
where EcountryDBID=207 order by EcountryRelatedKeyword.ProductCount desc,SortOrder) t11
union all
--越南--
select * from (select top 6 EcountrydbID,Keyword from EcountryRelatedKeyword
inner join Ehotsearchkeyword on EhotsearchkeywordID=Ehotsearchkeyword_ID
where EcountryDBID=231 order by EcountryRelatedKeyword.ProductCount desc,SortOrder) t12
union all
--澳大利亚--
select * from (select top 6 EcountrydbID,Keyword from EcountryRelatedKeyword
inner join Ehotsearchkeyword on EhotsearchkeywordID=Ehotsearchkeyword_ID
where EcountryDBID=14 order by EcountryRelatedKeyword.ProductCount desc,SortOrder) t13
open mycursor
fetch next from mycursor into @EcountrydbID,@Keyword
while (@@fetch_status=0)
begin
   if(@OldCountryDBID<>@EcountrydbID)
      begin
        set @OldCountryDBID=@EcountrydbID;
        set @OldCountryChoosedProductsIds='';
      end
   else
      begin
        if(@OldCountryChoosedProductsIds='')
           set @OldCountryChoosedProductsIds=Convert(varchar,@EproductId);
         else
           set @OldCountryChoosedProductsIds=@OldCountryChoosedProductsIds+','+Convert(varchar,@EproductId);
           
           print @OldCountryChoosedProductsIds;
      end
   declare @oldKeyWord varchar(200)
   set @oldKeyWord=@Keyword;
   exec CD_DealWithKeyWord @Keyword output
   --print @Keyword
   select top 1 @EproductId=EproductId,@PicPath=PicPath from Eproduct_search where EcountryDBID=@EcountrydbID and contains(keyword,@Keyword)
   and EproductId not in (select col from dbo.ecvv_split(@OldCountryChoosedProductsIds,','))
   order by companylevel asc,ecvvpv desc,last_update_datetime asc
   insert into EGlobalChannelHotSearch values(@EcountrydbID,@oldKeyWord,@EproductId,@PicPath,getdate());
   print @EproductId;

   fetch next from mycursor into @EcountrydbID,@Keyword
end
close mycursor
deallocate mycursor