---
title: "游标笔记"
author: "Eileenlml"
date: "2017-05-26T17:23:14Z"
tags: []
categories: ["数据库"]
slug: "untitled"
draft: false
lastmod: "2017-05-26T17:23:14Z"
id: 2376564
url: "https://eileenlml.iteye.com/blog/2376564"
---

cursor游标

定义游标：cursor 游标名 is select 语句：

打开游标：open 游标名

提取游标：fetch 游标名 into 变量名

关闭游标：close 游标名

 

declare

    cursor cur\_1 is

           select tid,title from logtypes ; ---------游标的声明

    v\_tid   logtypes.tid%type;

    v\_title  logtypes.title%type; ---------定义接收游标中的数据变量

    v\_typercd cur\_1%rowtype ; --------通过记录来接受数据

begin

    open cur\_1; -------打开游标

    loop

      fetch cur\_1 into v\_tid,v\_title ; ------取游标中的数据,遍历游标中的下一行数据

      exit when cur\_1%notfound ; -------检测是否已经达到最后一行

      dbms\_output.put\_line('读取tid='||v\_tid||' title='||v\_title); --------输出游标中的数据

    end loop;

    close cur\_1; --------关闭游标

 

    open cur\_1; -------打开游标

    loop

      fetch cur\_1 into v\_typercd ;

      exit when cur\_1%notfound ;

      dbms\_output.put\_line('--//读取tid='||v\_typercd.tid||' title='||v\_typercd.title);

    end loop;

    close cur\_1; ------关闭游标

   

    -----for循环游标

    for tmp\_record in cur\_1 loop

      dbms\_output.put\_line('++//读取tid='||tmp\_record.tid||' title='||tmp\_record.title);

    end loop;

 

end;