SqlServer 存储过程,数组类型的参数

存储过程是不支持数组参数的,但是有一个需求,是一个ID类型的数组,需要将字符串参数转换成一个数组,转换成一个表是最为合适的方法了。

declare @IDs VARCHAR(200)
set @IDs = '112,223,345,445,667';

declare @tmpID varchar(100), @tmpPos int;
	declare @IDTable table(ID varchar(20));

while CHARINDEX(',', @IDs) > 0
begin
  select @tmpPos = CHARINDEX(',', @IDs);
  select @tmpID = SUBSTRING(@IDs, 1, @tmpPos - 1);
  insert @IDTable (ID) values (@tmpID);
  select @IDs = SUBSTRING(@IDs, @tmpPos + 1, LEN(@IDs) - @tmpPos);
end
if LEN(@IDs) > 0
  insert @IDTable (ID) values (@IDs);
select * from @IDTable
posted @ 2025-02-27 12:45  nil  阅读(53)  评论(0)    收藏  举报