sql存储过程如何将1,2,3这种字符做批量操作

第一种
begin
declare @strid nvarchar(max)='296,297'

declare @temp Table (a varchar(100)) --创建临时表

--把参数@strid分割成int数组并插入临时表@temp
Declare @i Int
Set @strid = RTrim(LTrim(@strid))
Set @i = CharIndex(',',@strid)
While @i >= 1
Begin
Insert @temp Values(Left(@strid,@i-1))
Set @strid = SubString(@strid,@i+1,Len(@strid)-@i)
Set @i = CharIndex(',',@strid)
End
If @strid <> ''
Insert @temp Values (@strid) --插入临时表

select * from @temp


update anchor_Album set isShow=0 where id in(select * from @temp)
select *from AnchorInfo where useridx=100000022
select *from anchor_Album where useridx=100000022
end

第二种

///写一个函数

Create function [dbo].[f_split](
@col varchar(2000),--要分割的字符串
@split varchar(2)--以什么分割(,)
)
returns @t table(col varchar(20))
as
begin
while(charindex(@split,@col)<>0)
begin
insert @t(col) values (substring(@col,1,charindex(@split,@col)-1))
set @col = stuff(@col,1,charindex(@split,@col),'')
end
insert @t(col) values (@col)
return
end
GO

///用函数来切

declare @sql varchar(max)='68689782,75257988'
declare @tabele table([sId] int,useridx int)

insert into @tabele
select row_number()over(order by @@spid) as [sId], col as useridx from dbo.f_split(@sql,',')

原文地址:https://www.cnblogs.com/yjm8023/p/9324041.html