sql 分割字符串 存储过程
默认是用 , 分割,如有需要可以自己更改
我写的是 循环插入数据 存储过程,也可以自己改成 方法使用
存储过程:
alter proc proc_insert_myjyrecord(@userids varchar(300),@jrid int) as begin declare @location int declare @start int declare @seed int declare @split varchar(2) declare @tempuserid int set @userids=ltrim(rtrim(@userids)) set @start=1 set @split=',' set @seed=len(@split) set @location=charindex(@split,@userids) while @location<>0 begin set @tempuserid=substring(@userids,@start,@location-@start) insert into MyJyRecord(userid,jyrecordid,viewcount,downlondcount) values(@tempuserid,@jrid,0,0) set @start=@location+@seed set @location=charindex(@split,@userids,@start) end end
调用示例:
declare @str varchar(5000) set @str='666,555,' exec proc_insert_myjyrecord @str,1