--declare @RoleList varchar(1000)
declare @RoleID int
--set @RoleList='23,22,35,40,'
declare @EndIndex int
declare @StartIndex int
set @StartIndex=1
if right(@RoleList,1)<>','
begin
set @RoleList=@RoleList+','
end
while @StartIndex<=len(@RoleList)
begin
set @EndIndex=charindex(',',@RoleList,@StartIndex);
set @RoleID=cast(substring(@RoleList,@StartIndex,@EndIndex-@StartIndex) as int)
set @StartIndex= @EndIndex+1;
insert into #userRole(RoleID)
values(@RoleID)
end
本文介绍了一种使用T-SQL在循环中批量插入角色ID的方法。通过字符串操作和动态索引来解析逗号分隔的角色列表,并将其转换为整数类型进行数据库插入。此技巧适用于需要高效处理大量数据的情况。
1491

被折叠的 条评论
为什么被折叠?



