日期:2014-05-17  浏览次数:20459 次

SQL存储过程高效分割传入字符串方法!求更好的、更高效的方法!!!!!!!!
SQL code


declare @next int 
declare @ArrayLen int
declare @errorSun int
set @errorSun=0 
BEGIN TRANSACTION--开始事务
set @ArrayLen=dbo.Get_StrArrayLength(@DFXStr,'$')
 set @next=1
 while @next<=@ArrayLen-2
   begin
if(@Step=1)
update DFXCompSpread set RDFeedback=dbo.Get_StrArrayStrOfIndex(@DFXStr,'$',@next+2),rdchange=dbo.Get_StrArrayStrOfIndex(@DFXStr,'$',@next+1) where  DFXCSID=dbo.Get_StrArrayStrOfIndex(@DFXStr,'$',@next)
else
update DFXCompSpread set DFXComComment=dbo.Get_StrArrayStrOfIndex(@DFXStr,'$',@next+2), DFMChange=dbo.Get_StrArrayStrOfIndex(@DFXStr,'$',@next+1) where  DFXCSID=dbo.Get_StrArrayStrOfIndex(@DFXStr,'$',@next)



用 dbo.Get_StrArrayLength 发现字符串长了,更新起来特别慢,贴个串:
SQL code
'5099$Pass$阿萨德发生的$5003$Pass$阿发的是发生 $5009$Pass$$5004$Pass$$5031$Pass$$5041$Pass$$5039$Pass$$5058$Pass$$5005$Pass$$5008$Pass$$5018$Pass$$5038$Pass$$5084$Pass$$5002$Pass$$5045$Pass$$5022$Pass$$5019$Pass$$5036$Pass$$5042$Pass$$5046$Pass$$5066$Pass$$5043$Pass$$5050$Pass$$5014$Pass$$5016$Pass$$5021$Pass$$5054$Pass$$5060$Pass$$5069$Pass$$5013$Pass$$5065$Pass$$5006$Pass$$5007$Pass$$5010$Pass$$5011$Pass$$5015$Pass$$5017$Pass$$5024$Pass$$5025$Pass$$5027$Pass$$5033$Pass$$5035$Pass$$5044$Pass$$5048$Pass$$5070$Pass$$5075$Pass$$5078$Pass$$5061$Pass$$5062$Pass$$5053$Pass$$5023$Pass$$5032$Pass$$5076$Pass$$5012$Pass$$5020$Pass$$5029$Pass$$5030$Pass$$5034$Pass$$5037$Pass$$5040$Pass$$5047$Pass$$5049$Pass$$5051$Pass$$5052$Pass$$5059$Pass$$5063$Pass$$5067$Pass$$5068$Pass$$5071$Pass$$5072$Pass$$5073$Pass$$5074$Pass$$5077$Pass$$5079$Pass$$5080$Pass$$5082$Pass$$5083$Pass$$5086$Pass$$5087$Pass$$5090$Pass$$5091$Pass$$5092$Pass$$5093$Pass$$5064$Pass$$5088$Pass$$5089$Pass$$5026$Pass$$5028$Pass$$5056$Pass$$5098$Pass$$5055$Pass$$5057$Pass$$5081$Pass$$5085$Pass$$5094$Pass$$5095$Pass$$5096$Pass$$5097$Pass$$'




谁有更好的sql分割串的方法!!!!!!!!求教!!!!!!

------解决方案--------------------
不是分割慢.而是 你 循环需修改慢..
------解决方案--------------------
dbo.Get_StrArrayLength 干什么用?
求字符串长度可以用 LEN(字符串) 来获取
------解决方案--------------------
SQL code


CREATE  function dbo.f_splitstr
(   
  @SourceSql varchar(8000),   
  @StrSeprate varchar(10)   
)
returns @temp table(F1 varchar(200))   
as  
begin  
  declare @i int  
   set @SourceSql=rtrim(ltrim(@SourceSql))   
   set @i=charindex(@StrSeprate,@SourceSql)   
   while @i>=1   
   begin  
     insert @temp values(left(@SourceSql,@i-1))   
     set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)   
     set @i=charindex(@StrSeprate,@SourceSql)   
   end  
   if @SourceSql<>''  
     insert @temp values(@SourceSql)   
   return
end

------解决方案--------------------
靠 3 4 5 楼呢?
------解决方案--------------------
是吗?呵呵……
------解决方案--------------------
SQL code
--创建函数:
 

CREATE   function   [dbo].[SplitStr]
 (
     @SourceSql   varchar(8000),
     @StrSeprate   varchar(100))
     returns   @temp   table(F1   varchar(100)
 )   
   as     
   begin   
   declare   @ch   as   varchar(100)   
   set   @SourceSql=@SourceSql+@StrSeprate     
   while(@SourceSql<>'')   
                   begin   
                   set   @ch=left(@SourceSql,charindex(',',@SourceSql,1)-1)   
   insert   @temp   values(@ch)   
   set   @SourceSql=stuff(@SourceSql,1,charindex(',',@SourceSql,1),'')   
                   end   
  return   
   end
 

--应用(存储过程):
 
CREATE PROCEDURE [dbo].[UP_Student_GetListByStatus]
     @StatusList varchar(max)
 AS 
 SELECT [Stude