BEGIN
DECLARE @idx AS INT;--循环开始数字
DECLARE @code varchar(100);--用于存储查询临时表中字段值的临时变量(多个字段需要定义多个变量)
DECLARE @table_1 TABLE (usercode varchar(100));--定义临时表和表中的字段
INSERT INTO @table_1
select USER_CODE from 表名 where REALNAME='张三'--在临时表中插入数据
declare @count int select @count=count(*) from @table_1--临时表中数据总条数,也是循环结束数字
set @idx=1;
WHILE @idx <= @count
BEGIN--开始循环
select
@code=usercode--用临时表中字段值给临时变量赋值
from (select row_number() over( order by usercode)num,* from @table_1) a where num=@idx;
INSERT INTO [表名] ([字段1]) VALUES (@code) --需要进行的操作 insert update 等
SET @idx = @idx + 1; --循环++
END--结束循环
End
Sql Server 中循环的使用
最新推荐文章于 2024-04-12 16:50:10 发布