SQL serveri does not need a cursor, so it is suitable for instances with circular DML (update delete insert), serveridml

Source: Internet
Author: User

SQL serveri does not need a cursor, so it is suitable for instances with circular DML (update delete insert), serveridml

SQL serveri does not need a cursor, so it is suitable for instances in the case of circular 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,* INTO testdataFROM  sys.tablesSELECT row_number()over(ORDER BY a.storeid) IDD,* INTO #tbl_CursorFROM  testdata where id%3=0set @tbl_rows=@@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 output deleted.* into #tbl_Value WHERE id=@tbl_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 join #tbl_Value on #tbl_Value .id=t1.idselect * from testdata insert into testdata select *,........  from #tbl_Valueselect * from testdataSET @tbl_rows=@tbl_rows-1END 


Related Article

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.