分割字符串后,将值插入表中

时间:2012-12-17 07:17:15

标签: sql sql-server function stored-procedures

我想在employee表中插入值。 这些值采用字符串格式~分隔

例如:AA~B~123

我正在使用以下功能

拆分它
CREATE FUNCTION [db_owner].[FN_Split] (@String varchar(8000), @Delimiter char(1))
   returns @temptable TABLE (items 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(Items) values(@slice)        

           set @String = right(@String,len(@String) - @idx)        
           if len(@String) = 0 break        
       end    
   return        
   end 

现在我将输出作为

SELECT * FROM db_owner.FN_Split('AA~B~123','~')

输出

items
______
AA
B
123

现在我被困在这里

如何在employee表中插入上述值???

insert into employee (name,add,phone)
values('AA','B','123');

请指导。

试过这个但没有工作

insert into employee
SELECT * FROM db_owner.FN_Split('AA~BB~CC','~')

ERROR

Msg 213, Level 16, State 1, Line 1
Column name or number of supplied values does not match table definition.

3 个答案:

答案 0 :(得分:3)

您正在使用字符串拆分功能将项目作为行返回。您需要一个将它们作为列返回的函数。

或者您可以直接在查询中执行此操作。也许是这样的。

declare @S varchar(10) = 'AA~B~123'

select left(@S, T1.Pos - 1) as Col1,
       substring(@S, T1.Pos+1, T2.Pos-T1.Pos-1) as Col2,
       substring(@S, T2.Pos+1, len(@S)-T2.Pos) as Col3
from (select charindex('~', @S)) as T1(Pos)
cross apply (select charindex('~', @S, T1.Pos+1)) as T2(Pos)

结果:

Col1       Col2       Col3
---------- ---------- ----------
AA         B          123

这是一个适用于SQL Server 2000的版本

declare @S varchar(10) 
set @S = 'AA~B~123'

select left(@S, T.Pos1 - 1) as Col1,
       substring(@S, T.Pos1+1, T.Pos2-T.Pos1-1) as Col2,
       substring(@S, T.Pos2+1, len(@S)-T.Pos2) as Col3
from (select T.Pos1,
             charindex('~', @S, T.Pos1+1) as Pos2
      from (select charindex('~', @S) as Pos1) as T
     ) as T

答案 1 :(得分:1)

如果您可以像这样在存储过程中添加一个小计数器,那么生活会更容易:

CREATE FUNCTION [db_owner].[FN_Split] (@String varchar(8000), @Delimiter char(1))      
   returns @temptable TABLE (orderId int,items varchar(8000))        
   as        
   begin        
       declare @idx int        
       declare @slice varchar(8000)        
       declare @orderId int = 0 --<added a counter

        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(orderId, Items) values(@orderId, @slice)        
           set @orderId = @orderId+1 --<increment the counter

           set @String = right(@String,len(@String) - @idx)        
           if len(@String) = 0 break        
       end    
   return        
   end 

您的后续查询可能如下所示:

DECLARE @name varchar(50) = (SELECT items  FROM db_owner.FN_Split('AA~BB~CC','~') where orderId = 0)
DECLARE @add varchar(50) = (SELECT items  FROM db_owner.FN_Split('AA~BB~CC','~') where orderId = 1)
DECLARE @phone varchar(50) = (SELECT items  FROM db_owner.FN_Split('AA~BB~CC','~') where orderId = 2)
insert into employee 
    (
    name,
    add,
    phone
    )
values
    (
    @name, 
    @add,
    @phone
    )

但您是否尝试更改过程以便以横向格式输出数据而不是当前的垂直输出?

答案 2 :(得分:1)

请尝试此查询:

Insert into employee(col1,col2,col3) 
select substring_index('AA~B~123','~',1) as col1,substring_index(substring_index('AA~B~123','~',-2),'~',1) as col2,
substring_index(substring_index('AA~B~123','~',-1),'~',1) as col3