How to use sql2008 cursor forward_only STATIC

Source: Internet
Author: User
Tags id3

CREATE TABLE #xms_staff_department (id int, name varchar (+), parent_id int, Parent_path varchar (depth), small int, is_delete tinyint, sort smallint) INSERT into #xms_staff_departmentSELECT id,name,0, ' 0, ' +convert (Varchar,id), 1, ( case if Isvaild = 1 then 0 ELSE 1 END), Dorder from dept WHERE Pid = 0DECLARE @idINTDECLARE @parent_pathVARCHAR (+) DECL is @cursor1CURSORSET @cursor1 = CURSOR forward_only STATIC forselect ID, parent_path from #xms_staff_departmentOPEN @curs  Or1 FETCH NEXT from @cursor1 to @id, @parent_path while (@ @FETCH_STATUS = 0) Begininsert into #xms_staff_departmentSELECT Id,name,pid, @parent_path + ', ' +convert (Varchar,id), 2, (case if Isvaild = 1 then 0 ELSE 1 END), Dorder from dept WHERE Pi D = @idDECLARE @cursor2CURSORDECLARE @id2INTDECLARE @parent_path2VARCHAR SET @cursor2 = CURSOR forward_only STATIC FO Rselect ID, parent_path from #xms_staff_department WHERE parent_id = @idOPEN the @cursor2 FETCH NEXT from the @cursor2 into @id2, @parent_path2 while (@ @FETCH_status = 0) BEGIN INSERT into #xms_staff_departmentSELECT id,name,pid, @parent_path + ', ' +convert (Varchar,id), 3, (case When isvaild = 1 then 0 ELSE 1 END), the Dorder from dept WHERE Pid = @id2DECLARE @cursor3CURSORDECLARE @id3INTDECLARE @parent _path3varchar SET @cursor3 = CURSOR forward_only STATIC forselect ID, parent_path from #xms_staff_department WHERE par ent_id = @id2OPEN @cursor3 FETCH NEXT from @cursor3 to @id3, @parent_path3 while (@ @FETCH_STATUS = 0) Begininsert into #x Ms_staff_departmentselect id,name,pid, @parent_path + ', ' +convert (Varchar,id), 4, (case if Isvaild = 1 then 0 ELSE 1 END), Dorder from dept WHERE Pid = @id3DECLARE @cursor4CURSORDECLARE @id4INTDECLARE @parent_path4VARCHAR () SET @cursor4 = Curs  OR forward_only STATIC forselect ID, parent_path from #xms_staff_department WHERE parent_id = @id3OPEN @cursor4 FETCH NEXT From @cursor4 to @id4, @parent_path4 while (@ @FETCH_STATUS = 0) Begininsert into #xms_staff_departmentSELECT id,name,pid , @parent_path + ', ' +convert (VARchar,id), 5, (case is Isvaild = 1 then 0 ELSE 1 END), Dorder from dept WHERE Pid = @id4FETCH NEXT from @cursor4 into @id 4, @parent_path4ENDCLOSE @cursor4DEALLOCATE @cursor4FETCH NEXT from the @cursor3 into @id3, @parent_path3 endclose @cursor3DE ALLOCATE @cursor3FETCH next from @cursor2 to @id2, @parent_path2ENDCLOSE @cursor2DEALLOCATE @cursor2FETCH next from @cur Sor1 to @id, @parent_path endclose @cursor1DEALLOCATE @cursor1select * from #xms_staff_department
drop table #xms_staff_department

The above is a simple method of using SQL cursors, the writing is very water, but it is important to note that

SET @cursor = cursor forward_only STATIC for "Callout red use"

Note: Efficient execution of static cursors for efficient execution of SQL cursors

The static cursor creates a temporary copy of the data that will be used by the cursor, and all requests to the cursor are answered from this temporary table in tempdb, so the changes that are made to the base table are not reflected in the data returned when the cursor is fetched, and the cursor does not allow modification

FORWARD_ONLY specifies that the data can only be from the first bar to the last one

How to use sql2008 cursor forward_only STATIC

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.