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 に答える