SQLSERVER 储存过程操作多行数据的几种方式

对Table 表做操作 Table Table(表名),id(Table的自增字段),临时表的定义

  1. 用ID递增 去循环
    declare @id int =0
    while( (select top 1 id from Table where id>@id order by id)>0 )
    begin
     select @id=(select top 1 ostid from OutStockTask where ostid>@id order by ostid)--递增id 类似于for 中的i++
     select @id
    end

     

  2. 用临时表加while语句去循环
    declare @id int =0
    select id into #temp_Table from Table --创建临时表 
    while(exists(select * from Table where id not in(select id from #temp_Table)))--排除已操作过的数据id
    begin
        select @id=id from Table where not in(select id from #temp_Table) order by id--得到要操作的行数据id
        insert into #temp_Table--把做过操作的数据给到临时表
        select @id
    end

     

  3. 使用游标
posted @ 2020-09-29 16:59  龙er飞  阅读(540)  评论(0编辑  收藏  举报