--基本语句
--语句
declare --定义变量
i number;
str varchar2(20);
begin --语句块
i:=10;
str:='你好';
str:='小红'||str;
--打印输出
dbms_output.put_line(str);
end;
--输入内容
declare
s varchar2(20);
begin
s:='&输入您的姓名';--在字符串前面加上&符号,会弹出一个输入框,要求输入内容
dbms_output.put_line('您的姓名是:'||s);
end;
select * from sst;
--取查询的值
declare
c number;
begin
select count(1) into c from sst;
dbms_output.put_line('总人数是:'||c);
end;
--%type,%rowtype
declare
n sst.stuname%type;
r sst%rowtype;
begin
select stuname into n from sst where stuid=1007;
select * into r from sst where stuid=1007;
dbms_output.put_line('姓名:'||n||'性别:'||r.stuname);
end;
--语句
--if
--输入一个分数
--80-100 打印A
--60-80 打印b
--60以下 打印c
--if 条件 then else end if
declare
n number;
begin
n:='&请输入分数';
if(n>100 or n<0) then
dbms_output.put_line('分数有误!');
elsif(n>80) then
dbms_output.put_line('A');
elsif(n>60 ) then
dbms_output.put_line('B');
else
dbms_output.put_line('C');
end if;
end;
--case语句
--用法一:范围判断
declare
n number;
begin
n:='&请输入一个数';
case
when n>100 or n<60 then
dbms_output.put_line('分数不正确');
when n>80 then
dbms_output.put_line('A');
when n>60 then
dbms_output.put_line('B');
else
dbms_output.put_line('C');
end case;
end;
--用法二:值判断
declare
d number;
begin
d:='&输入天数';
case d
when 31 then
dbms_output.put_line('大月');
when 30 then
dbms_output.put_line('小月');
when 28 then
dbms_output.put_line('二月');
else
dbms_output.put_line('不对');
end case;
end;
--循环
--三种 while 、loop 、for
--1+。。。+100=?
--1.loop
declare
n number;
su number;
begin
n:=1;
su:=0;
loop
su:=su+n;
n:=n+1;
exit when n>100;
end loop;
dbms_output.put_line(su);
end;
--2.while
declare
n number;
s number;
begin
n:=1;
s:=0;
while n<=100
loop
/* if n=50 then
n:=n+1;
continue;--oracle10g 没有continue关键字,oracle11g有
end if;*/
--if n=50 then
--exit;
--end if;--当i=50时,退出循环
s:=s+n;
n:=n+1;
end loop;
dbms_output.put_line(s);
end;
--3.for
declare
s number;
begin
s:=0;
for n in 1..100
loop
s:=s+n;
end loop;
dbms_output.put_line(s);
end;
--游标 cursor
--把学生表中的数据用游标读取出来
declare--创建
cursor c is select * from sst; --定义一个游标指向结果集
r sst%rowtype;
begin
--2.打开
open c;
--3.读取数据
loop
fetch c into r;
--判断一下,如果没有读到数据的话退出循环
exit when c%notfound;
--把读取到的数据打印出来
dbms_output.put_line('编号:'||r.stuid||'姓名:'||r.stuname);
end loop;
--4.关闭游标
close c;
end;
--ref游标也是游标,是一种用户自定义类型的游标
--结果集的指定是不需要再定义游标对象时指定,
declare
type myCursor is ref cursor;--定义一个myCursor类型的游标类型
--用我的游标类型创建一个对象
mc myCursor;
i number;
r sst%rowtype;
begin
i:='&选择1或2用游标查询学生表或成绩表';
if i=1 then
--打开游标,指向学生表
open mc for select * from sst;
--遍历游标中的数据,打印
loop
fetch mc into r;
exit when mc%notfound;
dbms_output.put_line('学号:'||r.stuid||' 姓名:'||r.stuname);
end loop;
else
--打开游标,指向成绩表
dbms_output.put_line('此表还没建');
end if;
end;
--存储过程
create or replace procedure up_sum
--参数列表
(
a number:=10,
b number:=20
)--如果没有参数,括号都不需要
is
--过程体中的局部变量
s number;
begin
--过程体
s:=a+b;
dbms_output.put_line('和:'||s);
end;
--调用过程
begin
--up_sum(10,20);
--up_sum(b => 20,a => 40);
--up_sum();
--up_sum(20);
--up_sum(b => 50);
end;
--写一个过程做一个登录操作,传学生姓名和编号当用户名与密码做登录
--返回一个登录结果的信息
create or replace procedure up_login
(
userName varchar2,--参数只需要类型不需要长度
pwd varchar2,
flag out number --输出参数,返回1代表登录成功,0代表登录失败结果表示
)
is
begin
select count(1) into flag from sst where stuname=username and pwd=stuid;
end;
declare
v number;
begin
up_login('小红','1006',v);
if(v>0) then
dbms_output.put_line('登录成功!');
else
dbms_output.put_line('登录失败!');
end if;
end;
select * from sst;
--输出参数做一个学生新增操作
--输出刚新增的学生的stuid
create or replace procedure up_insertStu
(
stuname in varchar2,--默认即为in,传入参数
sex varchar2,
birthday date,
remark long,
stuid out number
)
is
begin
insert into sst values(se_stuid.nextval,stuname,sex,birthday,remark);
select se_stuid.currval into stuid from dual;
end;
--调用存储过程做新增操作
--------------------------------------------
select se_stuid.nextval from dual;
select se_stuid.currval from dual;
--在这个数据库连接上,一次nextval都没有执行过的话,currval就是空的
--------------------------------------------
declare
sid number;
begin
up_insertstu(stuname => "刘莹",sex => "女",birthday =to_date('1993-10-09','yyyy-mm-dd'),remark => "zhe">,sid );
dbms_output.put_line('刚新增的学生id号是:'||sid);
end;
-----------------------------------------
--调用过程做新增操作
declare
sid number;
begin
up_insertstu('小黄','男',to_date('1990-01-11','yyyy-mm-dd'),'无',sid);
dbms_output.put_line('刚新增的学生的id号是:'||sid);
end;
--参数可以是 in(默认),out,in out
--in为输入,out为输出,in out为即可输入也可输出
--in(默认),输入参数,在过程体中可以用传进来的值
--out,输出参数,,过程体中给这个参数赋值,值可以带进去,不能接收外面通过该变量传过来的值
--in out,既可以接收外面传进来的值也可以将值带出去
--out和in out的区别
--①只有out
create or replace procedure up_demo
(
i number,
s out number
)
is
begin
s:=s+i;
end;
--调用
declare
a number;
begin
a:=10;
up_demo(10,a);
dbms_output.put_line(a);
end;
--------------------------------------
--②in out
create or replace procedure up_demo2
(
i number,
s in out number
)
is
begin
s:=s+i;
end;
--调用
declare
a number;
begin
a:=10;
up_demo2(10,a);
dbms_output.put_line(a);
end;
--①输出为空,因为没有接收到通过s传入的值
--②输出为20,因为a传给了s,s的值为10
-------------------------------------------
--oracle存储过程中不能查询结果集
--数据包:是一系列对象与操作的封装
--包的声明
create or replace package pk_ly as
num number:=1;--包中的全局变量
type myCursor is ref cursor;
procedure demo(a number,b number);--过程只有声明没有过程体的
end;
--包体:是包所声明的对象或操作的实现
create or replace package body pk_ly as
--包中声明了一个过程,实现这个过程
procedure demo(a number,b number)
is
s number;
begin
s:=a+b;
dbms_output.put_line('想加:'||s);
end;
end;
--调用
declare
c pk_ly.myCursor;
r sst%rowtype;
begin
open c for'select * from sst';
loop
fetch c into r;
exit when c%notfound;
dbms_output.put_line('编号:'||r.stuid||'姓名:'||r.stuname);
end loop;
end;
--存储过程要返回结果集的话,需要用游标指向结果集,然后返回游标才行的
create or replace procedure up_selectsst
(
c out pk_ly.myCursor
)
is
begin
--在过程体中用游标指向结果集
open c for'select * from sst';
end;
-------
declare
c pk_ly.myCursor;
r sst%rowtype;
begin
up_selectsst(c);
--遍历取c游标指向的数据。。。
loop
fetch c into r;
exit when c%notfound;
dbms_output.put_line('编号:'||r.stuid||'姓名:'||r.stuname);
end loop;
end;
Oracle基本语句
最新推荐文章于 2021-04-11 13:44:19 发布