sql循环使用
数据库中使用循环取某一个字段的值,#tempIDs为要循环的临时表,@id为循环取出来的值
declare @count int;--查询表中数据的总条数 declare @number int;--循环条件 declare @id int;---循环得到的ID select @count=count(*) from #tempIDs;--要循环的临时表 if(@count is not null and @count<>0)----如果表中有数据才可以进行循环 begin set @number=0; while @number <@count begin select @number = @number +1;----每循环一次循环条件+1 select @id =tempTB.ID from (select ROW_NUMBER() over (order by id) 'rowindex',* from #tempIDs) as tempTB where tempTB.rowindex = @number; end end
浙公网安备 33010602011771号