SQL Serveri without cursors, suitable for instance instances of cyclic DML (update delete insert)

Source: Internet
Author: User

SQL Serveri without cursors, suitable for instance instances of cyclic DML (update delete insert)

 Use TEMPDBGOIF exists (SELECT * from tempdb. sysobjects where id=object_id (' tempdb. #tbl_Cursor ')) DROP TABLE #tbl_Cursorif exists (SELECT * from tempdb. sysobjects where id=object_id (' tempdb. #tbl_Value ')) DROP table #tbl_Valuegoif object_id (N ' testdata ') is not nulldrop table Testdataselect row_number () over ( ORDER by A.storeid) id,* to Testdatafrom Sys.tablesselect row_number () over (order by A.storeid) idd,* into #tbl_CursorFR OM testdata where Id%3=0set @[email protected] @rowcountCREATE CLUSTERED INDEX cix_tbl_tmp on #tbl_Cursor (ID) SELECT * into #tbl_Value from #tbl_Cursor WHERE 1=2while @tbl_rows >0begintruncate TABLE #tbl_ValueDELETE from #tbl_Cursor OUTP  UT deleted.* into #tbl_Value WHERE [Email protected]_rows--select * from Testdataupdate top T1 set id=id+10000 from testdata T1 INNER join #tbl_Value on #tbl_Value. Id=t1.idselect * from testdata deleted top T1 from testdata t1 inner Joi n #tbl_Value on #tbl_Value. Id=t1.idselect * from testdata insert to TestData SelecT *,........ from #tbl_Valueselect * from Testdataset @[email protected]_rows-1end  


SQL Serveri without cursors, suitable for instance instances of cyclic DML (update delete insert)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.