4

我想将值插入到员工表中。并且这些值以字符串格式~分隔

例如: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

现在我被困在这里

如何在员工表中插入上述值???

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

请指导。

试过这个但不工作

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

错误

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

3 回答 3

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
于 2012-12-17T07:43:59.343 回答
2

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

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
    )

但是您是否尝试过更改程序,使其以水平格式输出数据,而不是您当前拥有的垂直输出?

于 2012-12-17T07:48:38.153 回答
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 
于 2012-12-17T07:55:55.427 回答