温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

达梦游标的简单使用

发布时间:2020-08-07 00:11:27 来源:ITPUB博客 阅读:509 作者:yanhengdoudou 栏目:数据库

1         显示游标

create table T1 ( sex varchar2 ( 10 ), name varchar2 ( 20 ));

insert into t1 values ( ' ' , ' 小刘 ' );

insert into t1 values ( ' ' , ' 小陈 ' );

insert into t1 values ( ' ' , ' 晓燕 ' );

insert into t1 values ( ' ' , ' 小红 ' );

commit ;

select * from t1 ;

达梦游标的简单使用

DECLARE

  CURSOR c_t1_cursor is select sex , name from t1 where sex= ' ' ;

 v_sex  t1 . sex %type ;

 v_name t1 . name %type ;

begin

  open c_t1_cursor ;

  loop

  fetch c_t1_cursor into v_sex , v_name ;

  exit when c_t1_cursor%notfound ;

  print ( v_name|| ' is ' ||v_sex );

  end loop ;

  close c_t1_cursor ;

end ;

  达梦游标的简单使用

注:游标的定义要在匿名块的定义部分定义,游标打开、提取数据、关闭都在执行部分。

2         参数游标

语法:

CURSOR cursor_name

[(parameter_name datatype, ...)]

IS

select_statement;

......

OPEN cursor_name(parameter_value,.....) ;

DECLARE

  CURSOR c_t1_cursor ( c_sex varchar2 ( 10 )) is select sex , name from t1 where sex=c_sex ;

 v_sex  t1 . sex%type ;

 v_name t1 . name%type ;

begin

  open c_t1_cursor ( ' ' );

  loop

  fetch c_t1_cursor into v_sex , v_name ;

  exit when c_t1_cursor%notfound ;

  print ( v_name|| ' is ' ||v_sex );

  end loop ;

  close c_t1_cursor ;

end ;

  达梦游标的简单使用

注:open c_t1_cursor(' ') 也可以改为 open c_t1_cursor ( &sex );

3         游标for 循环

语法:

FOR record_name IN cursor_name|select_statement LOOP

statement1;

statement2;

......

END LOOP;

begin

  for t1_record in ( select sex , name from t1 where sex= ' ' ) loop

   print ( t1_record . name|| ' is ' ||t1_record . sex );

  end loop ;

end ;

  达梦游标的简单使用

4         游标表达式

语法:

TYPE ref_type_name IS REF CURSOR [RETURN return_type];

cursor_variable ref_type_name;

ref_type_name :用于指定自定义类型名

RETURN :用于指定返回结果的数据类型

cursor_variable :用于指定游标变量名

DECLARE

  TYPE t1_cursor IS REF CURSOR ;

 my_cursor t1_cursor ;

 v_sex  t1 . sex%type ;

 v_name t1 . name%type ;

begin

  OPEN my_cursor FOR select sex , name from t1 where sex= ' ' ;

  LOOP

  FETCH my_cursor INTO v_sex , v_name ;

  EXIT WHEN my_cursor%NOTFOUND ;

  print ( v_name|| ' is ' ||v_sex );

  end loop ;

  close my_cursor ;

end ;

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI