/**//* --建表 create table student( recordid number(38), sid int not null , sname varchar2(50), sdate date, sage number(3) ); */ --删除表 --drop table student; --插入数据 /**//* set serveroutput on --允许服务器输出 declare maxrecords constant int:=100; i int:=1; begin for i in 1..maxrecords loop insert into student(sid,sdate)values(i,sysdate); end loop --dbms_output.put_line('成功录入数据!'); commit; end; */ --select * from student; --声明一个变量 /**//* declare pi constant number(9):=3.1415926; begin commit; end; */ --复合数据类型(常见的五种) --1 .使用 %type 定义变量 --为了让PL/SQL中变量的类型和数据表中的字段的数据类型一致,Oracle 9i提供了%type定义方法。 --这样当数据表的字段类型修改后,PL/SQL程序中相应变量的类型也自动修改. /**//* Declare mydate student.sdate%type; begin commit; end; */ --2. 定义记录类型变量 --将多个基本数据类型捆绑在一起的记录数据类型。 /**//* set serveroutput on declare type myrecord is record( sid int, sdate date); srecord myrecord; --声明一个自定义记录类型变量的实例 begin select sid,sdate into srecord from student where sid=68; dbms_output.put_line('ID: '|| srecord.sid ||'Date:'|| srecord.sdate); --'||': 它是字符串连接符. end; */ --3.使用 %rowtype 变量 --使用%type可以使变量获得字段的数据类型,使用%rowtype可以使变量获得整个记录的数据类型。 --比较两者定义的不同:变量名 数据表.列名%type,变量名 数据表%rowtype。 /**//* set serveroutput on Declare mytableRow student%rowtype; begin select * into mytableRow from student where sid=88; dbms_output.put_line(mytableRow.sid || mytableRow.sdate); end; */ --4.定义一维表类型变量 --表类型变量和数据表是有区别的,定义表类型变量的语法如下: -- ――――――――――――――――――――――――――――――――――――― -- type 表类型 is table of 类型 index by binary_integer; -- 表变量名 表类型; -- ――――――――――――――――――――――――――――――――――――― -- 类型可以是前面的类型定义,index by binary_integer子句代表以符号整数为索引, -- 这样访问表类型变量中的数据方法就是“表变量名(索引符号整数)”。 /**//* Declare type tabletype1 is table of varchar2(4) index by binary_integer; --定义一个字符型的一维数组 type tabletype2 is table of student.sid%type index by binary_integer;--定义了一个整数数型的数组 table1 tabletype1; --实例声明 table2 tabletype2; --实例声明 begin table1(1):='学生'; table1(2):='职员'; table2(1):=88; table2(2):=89; dbms_output.put_line(table1(1)||table2(1)); dbms_output.put_line(table1(2)||table2(2)); end; */ --5.定义多维类型表变量 --相当于定义多维数组. --注意在运行下面的语句前要在数据库中插入数据. Declare type tabletype1 istableof student%rowtype indexby binary_integer; table1 tabletype1; begin select*into table1(60) from student where sid=60; dbms_output.put_line(table1(60).sid ||table1(60).sdate); end;
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)