SQL Server 2008中的静态游标

在本文中,将介绍SQL Server 2008中的静态游标。
在静态游标的帮助下,我们可以找到第一行,下一行,最后一行或者指定的行。

首先,我们创建一个名为EMP的表,并在此表上应用游标:

创建表

create table emp(emp_id int,em_name varchar(10))

go

insert into emp

select 1,'d' union all

select 2,'e' union all

select 3,'f' union all

select 4,'mahi' union all

select 5,'gill' union all

select 6,'singh'

go

select * from emp

静态游标:

静态游标显示打开游标时的结果集。
通过静态游标你不能更新或者删除数据。
如果我们打开静态游标,然后我们对原始数据源进行更改,例如:假设我们在表中插入一行,
那么游标将不会反映表中所做的任何更改,例如:插入的行将不会显示。

语法:

Declare cursor_name cursor

static

for

SQL语句

其中:

cursor_nameis游标的名称。

static:是关键字,它表示游标作为静态。

SQL语句:是要使用游标执行的SQL语句。

静态游标声明:

declare static_cursor cursor

static for

select * from emp

打开静态游标:

open static_cursor

从静态游标获取第一行数据:

fetch first from static_cursor

关闭静态游标:

close static_cursor

删除静态游标:

deallocate static_cursor
日期:2020-06-02 22:18:10 来源:oir作者:oir