使用游标逐行更新数据

时间:2021-02-05 13:24:41

/**//*scroll表示可随意移动游标指        针(否则只能向前),dynamic表示可以读写游标(否则游标只读)*/

         declare @idoc int,@doc varchar(2000)
         set @doc='<NewDataSet>
           <Reserve>
            <key_seq>70373381</key_seq>
                    <type>AP</type>
            </Reserve>
            <Reserve>
            <key_seq>70373380</key_seq>
                   <type>BP</type>
            </Reserve>
           <Reserve>
            <key_seq>70373379</key_seq>
                    <type>CP</type>
            </Reserve>
            </NewDataSet>'

         exec sp_xml_preparedocument @idoc OUTPUT, @doc
         select * into #reserve from openxml (@idoc, '/NewDataSet/Reserve',2)
         with (key_seq nvarchar(20),type nvarchar(20))
           declare  @key_seq varchar(20),@type varchar(20)
         declare my_cursor cursor   for  select * from #reserve
         open my_cursor
          fetch next from my_cursor into @key_seq,@type
          while(@@fetch_status=0)
          begin
          update #reserve set type='MP'  where key_seq=@key_seq
 fetch next from my_cursor into @key_seq,@type
          end     

          close my_cursor
          deallocate my_cursor

         select * from #reserve
         drop table  #reserve