Split sql parameter on comma

前端 未结 4 825
春和景丽
春和景丽 2021-01-14 20:04

Below code works for \'Main Stream\' or \'Premium\' as parameter, however I am trying to make it work for both of them as you can see below, but it doesn\'t return any resul

4条回答
  •  忘掉有多难
    2021-01-14 20:31

    try:

    select id from dbo.split("24,25,26",',');
    

    so you need to to:

    select * FROM sales  
    where myCategory IN ( select id from dbo.split(@myParameter,",");
    

    and add split function in your database by running:

    Create FUNCTION [Split](@String varchar(8000), @Delimiter char(1))       
     returns @temptable TABLE (id varchar(8000))       
     as       
      begin       
          declare @idx int       
          declare @slice varchar(8000)       
    
          select @idx = 1       
              if len(@String)<1 or @String is null  return       
    
          while @idx!= 0       
          begin       
              set @idx = charindex(@Delimiter,@String)       
              if @idx!=0       
                  set @slice = left(@String,@idx - 1)       
              else       
                  set @slice = @String       
    
              if(len(@slice)>0)  
                  insert into @temptable(id) values(@slice)       
    
              set @String = right(@String,len(@String) - @idx)       
              if len(@String) = 0 break       
          end   
      return       
      end
      Go
    

提交回复
热议问题