sql serveri不用遊標,適合於迴圈DML (update delete insert)的場合執行個體,serveridml

來源:互聯網
上載者:User

sql serveri不用遊標,適合於迴圈DML (update delete insert)的場合執行個體,serveridml

sql serveri不用遊標,適合於迴圈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 


相關文章

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.