SQL cursor (cursor)

Source: Internet
Author: User

1. Cursors are useful when the data table has no ID (identity), but the cursor eats more memory, reduces available concurrency, consumes bandwidth, locks resources, and, of course, more code

2. If you can not use cursors, try to avoid using cursors, run out and then must be closed and released, try not to define the cursor on a large amount of data, try not to use the cursor to update the data

Cursor:global for--Global Cursors

Cursor:local for--Local Cursors

Local means that the lifetime of the cursor is visible only in batches or functions or stored procedures

Global means that the cursor is the context for a particular connection and is valid globally

--First step: DECLARE CURSOR DECLARE test_cursor cursor scroll forselect name from  dbo.aaopen test_cursor--open Cursor--second time execute declare @name nvarchar fetch NEXT from test_cursor into @name-Next row Select @name--third time close empty cursor close test_cursor--close deallocate test_ cursor--Empty/* only supports 6 mobile options, one to the first row (the last), the next line (next), the previous line (  PRIOR), and  a direct jump to a row (ABSOLUTE (n)).  Skip a few lines relative to the current (RELATIVE (3)) */

Looping data through Cursors

DECLARE test_cursor Cursor scroll forselect id,materialname from dbo.table_1open test_cursordeclare @c nvarchar (10) declare @name nvarchar (+) while @ @FETCH_STATUS =0beginfetch next from Test_cursor to @c, @nameselect @c,@ nameend--finally executes close test_cursordeallocate test_cursor

  

SQL cursor (cursor)

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.