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)