IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[StrToTable]') AND type = 'TF')
DROP FUNCTION [dbo].[StrToTable]
GO

CREATE FUNCTION [dbo].[StrToTable]
  (
      @id int,
      @originalStr      VARCHAR(8000), --要分割的字符串
      @split varchar(100)  --分隔符号
  )
 
  RETURNS @temp TABLE(Result VARCHAR(100),Result1 VARCHAR(100),Result2 VARCHAR(100),id int)
  AS
  BEGIN
      DECLARE @result AS VARCHAR(100);   --定义变量用于接收单个结果 
      declare @num int
      set @num=1
      if @originalStr=''
      return;
     SET @originalStr = @originalStr+@split;   
     INSERT @temp(id) VALUES(@id)    
     WHILE (@originalStr <> '')
     BEGIN
         SET @result = left(@originalStr, CHARINDEX(@split, @originalStr, 1)-1) ; 
       
         begin
        
         if @num=1
         update @temp set Result=@result
         else if @num=2
         update @temp set Result1=@result
         else if @num=3
         update @temp set Result2=@result
         end
         set @num=@num+1
         --STUFF()函数用于删除指定长度的字符，并可以在指定的起点处插入另一组字符。
         SET @originalStr = STUFF(@originalStr, 1, CHARINDEX(@split, @originalStr, 1), '');
         
     END 
     RETURN
 END