随笔 - 178  文章 - 2  评论 - 0  阅读 - 99381

oracle变量

sqlplus模式下:

var 变量名 变量类型;

例如:

  var name varchar2(50);

  select name into :name from t_user;

  select :name from dual;

切换用户后仍有效,断开sqlplus后无效;

 

declare声明变量和赋值:

declare

  name varchar2(50):='小明';

begin

  dbms_output.put_line(name);

end

/

 

锚定(将变量类型与表中字段类型绑定):

declare

  user_code t_user.user_code%type;

  user_name t_user.user_name%type;

  user_birth_day date;

begin

  select user_code,user_name,user_birth_day into user_code,user_name,user_birth_day from t_user where user_code=121;

  dbms_output.put_line(user_name);

end

/

 

复合变量:

declare

  type user_record is record

  (user_code number,

   user_name varchar2(50),

   user_birth_day date)

  rc user_record;

begin

  select * into rc from t_user where user_code=101;

  dbms_output.put_line(rc.user_name);

end;

/

使用%后:

declare

 rc t_user%rowtype;

begin

  select * into rc from t_user where user_code=101;

  dbms_output.put_line(rc.user_name);

end;

/

 

游标变量:

声明:初始化游标变量标示符,将游标变量标示符和一个子查询关联在一起

打开:根据子查询取数据库表中的数据填充内存上下文

获取:从游标内存上下文取值填充到record,游标内存上下文中的值每取出一行就丢弃一行

关闭:将游标内存上下文释放

游标属性:

游标名字%rowcount : 从游标内存上下文获得的行的数量

游标名字%found  : 从游标内存上下文获得了返回true

游标名字%notfound : 从游标内存上下文取不到数据返回true

游标名字%isopen :  游标如果打开返回true

declare

  cursor cs is select * fromn t_user; --声明

  rc cs%rowtype;

begin

  open cs; --打开

  loop

    fetch cs into rc; --获取

      exit when cs%notfound;

    dbms_output.put_line(rc.user_name);

  end loop;

  close cs; --关闭

end;

/

游标for循环(隐式声明、打开、获取、关闭):

declare

  cursor cs is select * fromn t_user; --声明,可省略

begin

  for rc in cs loop --cs可以直接使用子查询代替(select * fromn t_user)

    dbms_output.put_line(rc.user_name);

  end loop;

end;

/

 

带参数的游标:

declare

  cursor cs (t_usercode number) is select user_code,user_name,user_birth_day from t_user where user_code=t_usercode order by user_birth_day desc;

begin

  for rc in cs (2) loop

    dbms_output.put_line(rc.user_code||'--'||rc.user_name||'--'||rc.user_birth_day);

  end loop;

end;

/

 

:变量名是plsql外部变量使用方式,plsql内部变量直接使用即可

posted on   渐行渐远的那些人  阅读(1338)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示