SQL cursor, SQL Server cursor usage, instance

Source: Internet
Author: User

If object_id ('tempdb .. # temp1010') is not null
Drop table # temp1010
Go
If object_id ('tempdb .. # temp2020') is not null
Drop table # temp2020
Go
Create Table # temp2020 (
It_id nvarchar (100) null
);
Insert # temp2020 (it_id) values (1)
Insert # temp2020 (it_id) values (2)
Insert # temp2020 (it_id) values (3)
Create Table # temp1010 (
It_id nvarchar (100) null,
Pc_total nvarchar (100) null,
Note_total nvarchar (100) null
);
Declare
@ It_id nvarchar (100 ),
@ Pc_total nvarchar (100 ),
@ Note_total nvarchar (100 );

-- Declare a cursor

Declare maincursor cursor for select it_id from # temp2020

-- Open the cursor
Open maincursor

-- Extract data
Fetch next from maincursor into @ it_id
While @ fetch_status = 0
Begin
Begin
Set @ pc_total = n'000000'
Set @ note_total = n'000000'
Insert into # temp1010 values (@ it_id, @ pc_total, @ note_total)
End
Fetch next from maincursor into @ it_id
End

-- Close the cursor
Close maincursor
Deallocate maincursor

Select * from # temp1010

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.