SQL是一种基于集合的语言(a set-based language) ,他更擅长操作和提出一组数据,而不是对
数据进行一行一行的处理。
SQL is a set-based language ,meaning that is excels at mantipulating and retrieving
set of rows ,rather than performing single row-by-row processing.
如果你的程序里一定要一条一条的执行,那么一定要先考虑使用如while循环,子查询,
临时表,表变量等等,如果这些都不能满足要求,在考虑使用游标。
T-SQL中游标的生存周期:
1.用返回一个有效结果集的sql语句来定义一个游标。
a cursor is defined via a SQL statement that returns a valid result set.
2. 打开游标
3. 一旦游标被打开就可以从游标中每次取出一行数据,要根据游标的定义可以向前去数据或
向后取数据
the rows can be fetched moving forword or backword ,depending on the original cursor definition.
4. 根据游标的类型,数据可以被修改或者只能读。
5.最后,用完游标后,必须被显示的关闭,并且从内存中移除。
游标定义格式:
declare cursor_name cursor
[local|global]
[forword_only|scroll]
[static|keyset|dynamic|fast_forword]
[read_only| scroll_locks|optimistic]
[type_warning]
for select_statement[for update [of column[,...]]]
The select_statement argument is the query used to define the data within the cursor. Avoid
using a query that hasmore columns and rows than will actually be used, because cursors, while
open, are kept inmemory. The UPDATE [OF column_name [,...n]] is used to specify those columns
that are allowed to be updated by the cursor.