Infinity loop for cursor in gpglobal. gplog

Source: Internet
Author: User

The original code is as follows:

Begin

Declare

@ SQL _stmt nvarchar (300 ),

@ V_client_id int,

@ V_pgname varchar (1000 ),

@ V_logid int;

Declare C2 cursor for select pgname, client_id from gpglobal. gplog;

Open C2

Fetch next from C2 into @ v_pgname, @ v_client_id

While (@ fetch_status = 0)

Begin

Exec getnextsequencenumber @ v_logid out, 'gplog _ seq ';

Set @ SQL _stmt = 'Update gpglobal. gplog set log_id = '+ Cast (@ v_logid as varchar) + 'where pgname = ''' [email protected] _ pgname + ''' and

Client_id = '+ Cast (@ v_client_id as varchar );

Exec sp_exec_with_log 'db-1216 ', @ SQL _stmt;

Fetch next from C2 into @ v_pgname, @ v_client_id

End

Close C2

Deallocate C2

End

 

The above stored procedure will be executed infinitely, mainly because the cursor C2 is based on the table gpglobal. gplog, when the cursor C2 is looping, the code in the loop changes gpglobal. gplog data,

This will cause C2 to see a new record that has been changed. The record is continuously executed and cannot be stopped. You only need to define C2 as static. It will only see the record when the cursor is opened, which is equivalent to a snapshot at that time.

 

Declare C2 cursor static for select pgname, client_id from gpglobal. gplog;

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.