Ntext欄位拆分處理

2021-06-16 01:00:52 字數 1815 閱讀 6563

ifexists(select*fromdbo.sysobjectswhereid=object_id(n'[dbo].[p_split]')andobjectproperty(id,n'isprocedure')=1)dropprocedure[dbo].[p_split]go

/*--

ntext

字段拆分處理

按指定的分隔符,將

ntext欄位拆分成多條記錄 注意:處理過程固定以名為id的int型字段為主鍵 如果不滿足這個條件,則要對應的修改儲存過程

--鄒建2004.07--*/

/*--呼叫示例

--測試資料 createtabletb(idintidentity(1,1),content

ntext) inserttbselect'001,002' unionallselect'001,002,003,004,005,006,007,008,009,010' unionallselectreplicate('001,002,003,004,005,006,007,008,009,010',8000)

--呼叫儲存過程,進行拆分 execp_split'tb','content',',','id=3'

droptabletb--*/

createprocp_split@tbnamesysname,  --要處理的表名@fdnamesysname,  --text/

ntext欄位名@splitcharnvarchar(10)=',',--拆分的字串分隔符@wherenvarchar(1000)=''--要處理的記錄的條件asifisnull(@splitchar,'')=''set@splitchar=','

declare@snvarchar(4000)set@s='createtable#t(idintidentity(1,1),renvarchar(50))declare@idint,@ptrvarbinary(16)declare@snvarchar(4000),@iint,@jintdeclare@sp1varchar(10),@stepint

select@sp1=reverse(@splitchar),@step=len(@splitchar)

declaretbcursorlocalforselectid,s=substring(['+@fdname+'],1,4000)from['+@tbname+']wheredatalength(['+@fdname+'])>0 '+caseisnull(@where,'')when''then'' else'and('+@where+')'end+'

opentbfetchtbinto@id,@swhile@@fetch_status=0begin set@i=1 while@s<>'''' begin  iflen(@s)=4000   select@j=4000-charindex(@sp1,reverse(@s))    ,@i=@i+@j+@step    ,@s=left(@s,@j)  else   select@i=@i+4000,@j=len(@s)  insert#tselectsubstring(@s,id,charindex(@splitchar,@s+@splitchar,id)-id)  from序數表  whereid<=@j+@stepandcharindex(@splitchar,@splitchar+@s,id)-id=0  select@s=substring(['+@fdname+'],@i,4000)  from['+@tbname+']  whereid=@id end

fetchtbinto@id,@sendclosetbdeallocatetbselect*from#t'execsp_executesql@s ,n'@splitcharnvarchar(10)' ,@splitchargo

Ntext欄位拆分處理

if exists select from dbo.sysobjects where id object id n dbo p split and objectproperty id,n isprocedure 1 drop procedure dbo p split go ntext欄位拆分處理 ...

Ntext欄位拆分處理

if exists select from dbo.sysobjects where id object id n dbo p split and objectproperty id,n isprocedure 1 drop procedure dbo p split go ntext欄位拆分處理 ...

Ntext欄位拆分處理

if exists select from dbo.sysobjects where id object id n dbo p split and objectproperty id,n isprocedure 1 drop procedure dbo p split go ntext欄位拆分處理 ...