永发信息网

游标的详细用法

答案:2  悬赏:0  手机版
解决时间 2021-04-05 03:13
  • 提问者网友:火车头
  • 2021-04-04 16:18
游标的详细用法
最佳答案
  • 五星知识达人网友:荒野風
  • 2021-04-04 16:40
declare @id int
declare @name varchar(50)
declare cursor1 cursor for --定义游标cursor1
select * from table1 --使用游标的对象(跟据需要填入select文)
open cursor1 --打开游标
fetch next from cursor1 into @id,@name --将游标向下移1行,获取的数据放入之前定义的变量@id,@name中
while @@fetch_status=0 --判断是否成功获取数据
begin
update table1 set name=name+'1'
where id=@id --进行相应处理(跟据需要填入SQL文)
fetch next from cursor1 into @id,@name --将游标向下移1行
end
close cursor1 --关闭游标
deallocate cursor1
全部回答
  • 1楼网友:我住北渡口
  • 2021-04-04 17:33
这是游标的知识点,如果已经看过可跳过,直接看下面的例子:
游标提供了一种对从表中检索出的数据进行操作的灵活手段,就本质而言,游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。游标总是
与一条T_SQL
选择语句相关联因为游标由结果集(可以是零条、一条或由相关的选择语句检索出的多条记录)和结果集中指向特定记录的游标位置组成。当决定对结果集进行处理
时,必须声明一个指向该结果集的游标。 使用游标有四种基本的步骤:声明游标、打开游标、提取数据、关闭游标。 1.声明游标 同其它变量一样,我们也可以定义游标的访问类型:全局、共享、实例或局部,游标变量的命名规范建议也同其它变量一样。 declare my_cursor cursor keyset for select * from info
DECLARE CustomerCursor CURSOR FOR
SELECt acct_no,name,balance FROM customer WHERe province="北京"; 2.打开游标
OPEN CustomerCursor; 3.提取数据
FETCH语句来取得数据。一条FETCH语句一次可以将一条记录放入程序员指定的变量中。已经声明并打开一个游标后,我们就可以将数据放入任意的变量中。在FETCH语句中您可以指定游标的名称和目标变量的名称。--声明局部变量
declare @id int,@name varchar(20),@address varchar(20)
--定位到指定位置的记录
fetch absolute 56488 from my_cursor into @id,@name,@address
select @id as id,@name as name,@address as address
--定位到当前记录相对位置记录
fetch relative -88 from my_cursor into @id,@name,@address
select @id as id,@name as name,@address as address
--定位到当前记录前一条
fetch prior from my_cursor into @id,@name,@address
select @id as id,@name as name,@address as address
--定位到当前记录后一条
fetch next from my_cursor into @id,@name,@address
select @id as id,@name as name,@address as address
--定位到首记录
fetch first from my_cursor into @id,@name,@address
select @id as id,@name as name,@address as address
--定位到尾记录
fetch last from my_cursor into @id,@name,@address
select @id as id,@name as name,@address as address
注:如果提示“提取类型··不能用于只进游标”则在声明游标时:DECLARE MyCursor CURSOR SCROLL FOR ·· (FAST_FORWARD :只进游标 ; SCROLL: 滚动游标 )示例:DECLARE MyCursor CURSOR SCROLL FOR SELECT * FROM Test1DECLARE @ROW1 int,@ROW2 char(10),@ROW3 char(10),@ROW4 char(10) OPEN MyCursor FETCH next FROM MyCursor INTO @ROW1, @ROW2, @ROW3, @ROW4WHILE (@@fetch_status =0)
BEGIN INSERT INTO Test1(id, a, b, c)
VALUES (@ROW1, @ROW2, @ROW3, @ROW4) FETCH next FROM MyCursor INTO @ROW1, @ROW2, @ROW3, @ROW4END
CLOSE MyCursor
DEALLOCATE MyCursor(示例是将Test1表中的数据复制一次!)(@@fetch_status 返回被 fetch 语句执行的最后游标的状态,而不是任何当前被连接打开的游标的状态。0 fetch 语句成功;-1 fetch语句失败或此行不在结果集中;-2 被提取的行不存在;所以使用@@fetch_status=0判断是否游标到了最后一行)高级应用:修改当前游标所在行的信息,我们可以如下操作:
UPDATe table1
SET balance=1000
WHERe CURRENT of yourCursor;
删除当前行的操作如下:
DELETE FROM table1
WHERe CURRENT OF yourCursor; 此时声明游标的时候:declare my_cursor cursor scroll dynamic for ···
scroll表示可随意移动游标指针(否则只能向前),dynamic表示可以读写游标(否则游标只读)
例子:
首先总结下:
游标按以下操作进行
parse 解析
bind 绑定
open 打开
execute 执行
fetch 回取
close 关闭
1.写自己第一个游标PL/SQL
declare
cursor c_s is select * from user_tables;
begin
open c_s; --打开游标
close c_s;--关闭游标
end;
游标的4个属性 %found,%notfound,%rowcount,%isopen
1.%found 游标有记录则返回true否则false
declare
cursor c_s is select * from user_tables;
cc c_s%rowtype;
begin
open c_s; --打开游标
fetch c_s into cc;
while c_s%found loop
fetch c_s into cc;
end loop;
close c_s;--关闭游标
end;

2.%notfound 游标没记录则返回true否则false(个人感觉有点多余)
declare
cursor c_s is select * from user_tables;
cc c_s%rowtype;//游标变量
begin
open c_s; --打开游标
fetch c_s into cc;
while c_s%found loop
exit when c_s%notfound;
fetch c_s into cc;
end loop;
close c_s;--关闭游标
end;
3.%rowcount 返回游标取回的记录数目
declare
cursor c_s is select * from user_tables;
cc c_s%rowtype;
begin
open c_s; --打开游标
fetch c_s into cc;
while c_s%found loop
dbms_output.put_line(c_s%rowcount);
fetch c_s into cc;
end loop;
close c_s;--关闭游标
end;
4.%isopen 如果游标打开就返回true 否则false
declare
cursor c_s is select * from user_tables;
begin
if c_s%isopen then
dbms_output.put_line('cursor is open');
else
open c_s; --打开游标
end if;
close c_s;--关闭游标
end;
游标参数
declare
cursor c_s(cs_id number) is select * from admin id=cs_id;
begin
open c_s(10); --打开带参数的游标
close c_s;--关闭游标
end;
游标中的for update
declare
cursor c_s is select id from admin
for update of id //查询时锁定 id列
begin
open c_s;
commit;//提交释放锁 或者可以用 rollback
close c_s;
end;
游标中的where cursor of
UPDATE table_name SET set_clause WHERe CURSOR OF cursor_name; //更新游标所指向的那条记录
DELETE FROM table_name WHERe CURSOR OF cursor_name; //删除游标所指向的那条记录
游标中的ref cursor类型
TYPE c_s IS REF CURSOR RETRUN table%rowtype; //这种形式为强类型 在声明的时候就定了为行类型
TYPE c_s IS REF CURSOR;//弱类型 不与记录的数据类型关联
例子:
declare
TYPE c_s IS REF CURSOR RETRUN table%rowtype;
TYPE c_s2 IS REF CURSOR;
var_cs c_s;//声明为游标变量的类型
begin
OPEN c_s FOR select * from admin;
CLOSE c_s;
end;
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯