学习pl/sql之一
--使用pl/sql语句打印一个hello world
begin
dbms_output.put_line('hello,world');
end;
但是在sqlplus里面就不一样了
首先输入
begin
dbms_output.put_line('hello,world');
end;
/
通过set serveroutput on
/
来调用输出语句
可以通过edit命令对过程语句进行修改
name [constrant]datatype[notnull]:=|default value
--pl/sql语句中的if语句
declare
sal1 number(6,2);
begin
select sal into sal1 from emp where lower(ename)=lower('&ename');
if (sal1<2000) then
dbms_output.put_line('less 2000');
end if;
end;
--pl/sql 里面的 if else 语句
declare
sal1 number(6,2);
begin
select sal into sal1 from emp where lower(ename)=lower('&ename');
if (sal1<2000) then
dbms_output.put_line('less 2000');
else
dbms_output.put_line('more 2000');
end if;
end;
--pl/sql语句中的if elsif语句
declare
sal1 number(6,2);
begin
select sal into sal1 from emp where lower(ename)=lower('&ename');
if (sal1<2000) then
dbms_output.put_line('less 2000');
elsif (sal1>2000 and sal1<400) then
dbms_output.put_line('between 2000 and 4000');
elsif (sal1>400) then
dbms_output.put_line('more 4000');
else
dbms_output.put_line('other...........');
end if;
end;
--pl/sql语句里面的case语句
declare
a number(2):=0;
begin
a:=&a;
case a
when 10 then
dbms_output.put_line('aaaaaaaaaaa');
when 20 then
dbms_output.put_line('bbbbbbbbbbbb');
else
dbms_output.put_line('other.....');
end case;
end;
--pl/sql语句里面的loop语句
declare
q number :=0;
begin
loop
q:=q+1;
dbms_output.put_line('q='||q);
exit when q=10;
end loop;
end;
--使用if 结束的
declare
q number :=0;
begin
loop
q:=q+1;
dbms_output.put_line('q='||q);
if q=10 then
exit;
end if;
end loop;
end;
--使用 while 循环
declare
q number :=0;
begin
while(q<10)loop
q:=q+1;
dbms_output.put_line('q='||q);
end loop;
end;
--使用 for循环 反向遍历 reverse
declare
begin
for i in reverse 1..10 loop
dbms_output.put_line(i);
end loop;
end;
PLS_INTEGER和BINARY_INTENER唯一区别是在计算当中发生溢出时,BINARY_INTENER型的变量会被自动指派给一个NUMBER型而不会出错,PLS_INTEGER型的变量将会发生错误。
record 定义类型
集合容器
index_by table
嵌套表
varray
type num_array is table of number(5) index by binary_integer;
单行单列 变量 varchar2 %type
单行多列 record
单列多行 集合 (type)
多行多列 集合(rowtype)
索引表的格式
type name is table of element_type index by key_type;
declare
v_ename emp.ename%type;
v_emp emp%rowtype;
begin
select ename into v_ename from emp where empno=7369;
select * into v_emp from emp where empno=7369;
dbms_output.put_line(v_ename);
dbms_output.put_line(v_emp.sal||' '||v_emp.ename);
end;
--仅输出特殊的结构 使用 record来定义
declare
type emp_record is record(
v_ename emp.ename%type,
v_sal emp.sal%type,
v_deptno emp.deptno%type
);
v_emp_record emp_record;
begin
select ename,sal,deptno into v_emp_record from emp where empno=7369;
--我也可以通过赋值语句修改里面的值
v_emp_record.v_ename:='zhaan';
dbms_output.put_line(v_emp_record.v_ename);
dbms_output.put_line(v_emp_record.v_sal);
dbms_output.put_line(v_emp_record.v_deptno);
end;
--
declare
type num_array is table of number(5) index by binary_integer;
a num_array;
begin
for i in 1..10 loop
a(i):=i;
end loop;
for i in 1..10 loop
dbms_output.put_line(a(i));
end loop;
end;
--例子一
declare
type emp_array is table of emp%rowtype index by binary_integer;
a emp_array;
begin
select * bulk collect into a from emp;
for i in a.first..a.last loop
dbms_output.put_line(a(i).ename||' '||a(i).job);
end loop;
end;
--集合中装有集合
declare
type record1 is record(
vempno emp.empno%type,
vename emp.ename%type,
vsal emp.sal%type
);
type record2 is record(
vdeptno emp.deptno%type,
vrecord record1
);
a record1;
b record2;
begin
select empno,ename,sal into a from emp where empno=7369;
b.vrecord:=a;
dbms_output.put_line(b.vrecord.vempno);
dbms_output.put_line(b.vrecord.vename);
dbms_output.put_line(b.vrecord.vsal);
end;
--
declare
type cc is table of varchar2(20) index by varchar2(20);
a cc;
begin
a('beijing'):='china';
a('dongjing'):='japan';
a('huashengdun'):='usa';
dbms_output.put_line(a('beijing'));
end;
学习pl/sql之一的更多相关文章
- Oracle 学习PL/SQL
先上一张实用的图:用于转义字符的. SQL> select chr(42) ||'is what?' from dual; CHR(42)||---------*is what? 想转义哪个就转 ...
- 使用PL/SQL Developer 学习pl/sql
1.创建表并且插入一些数据 (这里表名为test): 2. New 一个SQL Window敲下如下代码(--为注释部分): declare --declare:用于plsql中的声明变量,和be ...
- Oracle PL/SQL学习之Hello World(0)
1.PL/SQL是Oracle数据库的一大创举,让一些复杂繁琐的常规主流编程代码做的编码处理过程,只需要在PL/SQL中使用简短的几句代码就可以解决,并且准确高效.那么遵循惯例,我们学习PL/SQL编 ...
- Oracle PL/SQL实战代码下载
最近在学习PL/SQL编程,算是一个进阶吧,书没带光盘,所以按照书中的地址去下载样例,无法下载,到图灵官网找到了源代码下载地址,无法下载的留邮箱,我发给大家 下载地址: http://www.itur ...
- Oracle数据库之PL/SQL基础
介绍PL/SQL之前,先介绍一个图像化工具:Oracle SQL Developer 在oracle的开发过程中, 我们难免会使用第三方开发的软件来辅助我们书写SQL, pl/sql是一个不错的sql ...
- pl/sql编程
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- ORACLE PL/SQL开发--bulk collect的用法 .
刚刚在inthirties老大的博客里看到这篇文章,写的不错,正好自己最近在学习PL/SQL,转过来学习学习. ============================================ ...
- pl/sql 的 put和put_line区别
在学习PL/SQL脚本时,打印语句是用得最多的语句. 在Oracle中,又有两种打印的方法:put和put_line.它们的区别是:put:不换行输出,输出在缓冲区,不显示出来,直到执行put_lin ...
- PL/SQL程序中调用Java代码(转)
主要是学习PL/SQL调用JAVA的方法. 平台:WINDOWS 1.首先使用IDE写好需要调用的java代码,再添加"create or replace and compile java ...
随机推荐
- Linux下如何查看分区文件系统类型
1,fdisk -l fdisk -l 只能列出硬盘的分区表.容量大小以及分区类型,但看不到文件系统类型. 2,df -h df 命令是用来查看文件系统磁盘空间使用量的.但df 命令只会列出已挂载的文 ...
- Spring MVC 入门笔记
主要名词解释 DispatcherServlet 前端控制器 相当于一个转发器 入口: protected void doDispatch(HttpServletRequest request, H ...
- TinkerPop简述
ThinkerPop Apache 顶级项目 概述 TinkerPop是一个面向实时事务处理(OLAP)以及批量.分析型(OLTP)的开源的图计算框架.TinkerPop是一个可以应用于不同图形数据库 ...
- 20155237 2016-2017-2 《Java程序设计》第3周学习总结
20155237 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 第四章 认识对象 对象:存在的具体实体,具有明确的状态和行为. 类:具有相同属性和行为的一组 ...
- pythonDjango开发-创建django程序
1.创建jgango程序 a.命令 cmd命令行下 进入到需要创建项目的路径下 django-admin startproject mysite 创建项目 cd mysite 进入项目文件夹 pyt ...
- VirtualBOX启动错误the vm session was closed before any attempt to power it on解决办法
昨天晚上笔记本休眠后,今天早上启动vm时,报错了. 点击详细启动错误:the vm session was closed before any attempt topower it on. 解决办法: ...
- 05-session-会话跟踪技术
1.session简介 Django中默认支持Session,其内部提供了5种类型的Session供开发者使用: 数据库(默认) 缓存 文件 缓存+数据库 加密cookie Session是服务器端技 ...
- 2555: SubString
2555: SubString 链接 题意: 动态在末尾加入一个字符串,询问一个字符串出现了多少次. 分析: 如果没有动态加入,那么建出SAM后,求出parent树上,每个点|Right|,然后走一遍 ...
- 设置 idea 运行前不去检查其他类的错误的方法
问题描述 MainClass为要运行的正常类,目录下存在一个类ErrorClass有错误,运行MainClass时会无法运行. 现在需要忽略ErrorClass中的错误,执行MainClass中的代码 ...
- AWK高端功能-数组
第1章 awk命令基础 1.1 awk命令执行过程 1.如果BEGIN 区块存在,awk执行它指定的动作. 2.awk从输入文件中读取一行,称为一条输入记录.如果输入文件省略,将从标准输入读取 3.a ...