oracle的psql编写

oracle的psql编程

select * from user_indexes;

set serveroutput on
declare
name_ varchar2(20):='龚为明';
id_ Integer:=23;
begin
  for i in 1..20 loop
    if mod(i,2)=0 then
    dbms_output.put_line(name_);
    dbms_output.put_line('mod(i,2)='||mod(i,2));
    else
    dbms_output.put_line(id_);
    end if;
  end loop;
end;


--找出三个数中的最大数值
set serveroutput on
declare
a1 int:=1;
b1 int:=4;
c1 int:=3;
max_value int;
begin
if(a1>b1) then
  if (a1>c1) then
    max_value:=a1;
  else
    max_value:=c1;
  end if;
else
  if (b1>c1) then
    max_value:=b1;
  else
    max_value:=c1;
  end if;
end if;
 --dbms_output.put_line('max='||max_value);
  sop('max='||max_value);
end;

--求出所有奇数的和和偶数的和
set serveroutput on
declare
sum_value1 int:=0;
sum_value2 int:=0;
begin
for i in 1..100 loop
  if mod(i,2)=0 then
   sum_value1:= sum_value1+i;
  else
   sum_value2:= sum_value2+i;
  end if;
end loop;
  dbms_output.put_line('sum_value1='|| sum_value1);
  dbms_output.put_line('sum_value2='|| sum_value2);
end;


set serveroutput on
declare
sum_value int:=0;
i int:=1;
begin
  while i<100 loop
    i:=i+1;
    sum_value:=sum_value+i;
  end loop;
  dbms_output.put_line('sum_value='||sum_value);
end;

set serveroutput on
declare
sum_value int:=0;
i int:=0;
begin
  loop
    i:=i+1;
    if mod(i,2)=1 then
      dbms_output.put_line('奇数='||i);
    else
      dbms_output.put_line('偶数='||i);
    end if;
    sum_value:=sum_value+i;
  exit when i>100;--这是一个条件
  end loop;--不过不要忘了这里的
  dbms_output.put_line('sum_value='||sum_value);
end;


set serveroutput on
declare
i integer := &i;
begin
  loop
    i:=i+1;
    dbms_output.put_line('i='||i||':'||'hello oracle!');
    exit when(i>10);
  end loop;
end;

set serveroutput on
declare
score INTEGER:=70;
i integer:=1;
begin
  while i<90 loop
    i:=i+1;
    dbms_output.put_line('你好,我是你的人了--i='||i);
  end loop;
end;

select * from tab;
select * from student2;
desc student2;
insert into student2 values(10,'龚为明','男',1);
--开始操作我们的这个表中的数据
set serveroutput on
declare
stu_id int:=&a1;
name1 student2.stuname%type;
begin
--开始查询赋值
select stuname into name1 from student2 where stuid=stu_id;
dbms_output.put_line('name='||name1);
exception
--这个异常时系统自定义的异常,所以当复发了这个异常时,就会爆出这个异常提示
when no_data_found then
  dbms_output.put_line('没有找到该记录!');
end;


--no_data_found
--too_many_rows

--使用结构体进行赋值操作
set serveroutput on
declare
stu student2%rowtype;
--自定义异常
a1 int;
begin
stu.stuid:=&a1;
--开始查询赋值
select * into stu from student2 where stuid=stu.stuid;
dbms_output.put_line('name2='||stu.stuname||'sex='||stu.sex);
--自己定义的异常首先得激活才可以使用
a1:=9/0;
exception
  when no_data_found  then
    dbms_output.put_line('没有找到这个数据!');
  when others then
    dbms_output.put_line('其他异常');
end;

set serveroutput on
declare
--自定义一个异常
myexception exception;--这里定义一个异常
set serveroutput on
declare
stu student2%rowtype;
--自定义异常
a1 int;
begin
stu.stuid:=&a1;
--开始查询赋值
select * into stu from student2 where stuid=stu.stuid;
dbms_output.put_line('name2='||stu.stuname||'sex='||stu.sex);
--自己定义的异常首先得激活才可以使用
a1:=9/0;
exception
  when no_data_found  then
    dbms_output.put_line('没有找到这个数据!');
  when others then
    dbms_output.put_line('其他异常');
end;

set serveroutput on
declare
a1 int:=&a;
myexception exception;
begin
if(a1>=0) then
  dbms_output.put_line('这是一个正数');
else
  --激活异常,使用raise
  raise myexception;
end if;
--开始判断异常的类型属于哪一种
exception
when myexception then
  dbms_output.put_line('异常:这是一个负数,需要填一个正数!');
when others then
  dbms_output.put_line('异常:这个数无效');
end;

--================================================================
--开始编写存储过程!
--下面的代码全部是存储郭过程的代码
select * from student2;
set serveroutput on
declare
stu student2%rowtype;
begin
stu.stuid:=32;
stu.stuname:='龚小帅';
stu.sex:='女';
stu.dept_id:=1;
--添加学生
update student2 set stuid:=stu.stuid,stuname:=stu.stuname,sex:=stu.sex,dept_id:=stu.dept_id where stuid:=stu.stuid;
select stuid,stuname,sex,dept_id into
stu.stuid,stu.stuname,stu.sex,stu.dept_id from student2 where stuid=30;
dbms_output.put_line(stu.stuid||','||stu.stuname||','||stu.sex||','||stu.dept_id);
end;

--开始写存储过程
desc student2;
select * from student2;

--编写一个存储过程,指定编号的删除学生信息
create or replace procedure delProc(var_stuId in student2.stuid%type)
as
--存储过程中不需要使用我们的而这个返回值,函数中就需返回值
begin
delete from student2 where stuid=var_stuId;
exception
when no_data_found then
  dbms_output.put_line('没哟该记录存在');
end delProc;

select * from student2;
--调用该存储过程
set serveroutput on
declare
id1 int:=&a1;
begin
delProc(id1);
end;

insert into student2 values(100,'龚为明','男',1);
desc student2;
select * from student2;
--编写一个存储过程,用于插入学生数据
create or replace procedure addPro(stuInfo student2%rowtype)
as
begin
--这里开始插入数据
insert into student2 values(stuInfo.stuid,stuInfo.stuname,stuInfo.sex,stuInfo.sal,stuInfo.dept_id);
end addPro;
--调用该存储过程
set serveroutput on
declare
stuInfo student2%rowtype;
begin
--在这里开始自己赋值
--注意在这里赋值的时候,字符串赋值需要使用引号进行引号进行括起来
--这样才能够表示为字符串类型的数据,否则就会报错
stuInfo.stuid:=&a1;
stuInfo.stuname:=&a2;
stuInfo.sex:=&a3;
stuInfo.sal:=&a6;
stuInfo.dept_id:=&a4;
--dbms_output.put_line('stuname='||stuInfo.stuname);
--将这个结构体一个整体当做参数传递给存储过程,然后通过存储过程进行保存数据
addPro(stuInfo);
--在调用存储过程进行显示所有的值
proWithCursor();--这个存储过程用来输出使用的
end;

create or replace procedure proWithCursor
as
cursor proCursor is
select * from student2;
begin
for stuInfo in proCursor loop
  dbms_output.put_line(stuInfo.stuname||':'||stuInfo.sex||':'||stuInfo.sal);
end loop;
end;


set serveroutput on
create or replace procedure showAllInfo
as
cursor mycursor is
select * from student2;
begin
  for a1 in mycursor loop
  dbms_output.put_line(mycursor.stuname||':'||mycursor.sex||':'||mycursor.sal);
  end loop;
end;

--然后查询有么有这条数据
select * from student2;


create or replace procedure proc1(
id1 student2.stuid%type,
name1 student2.stuname%type,
sex1 student2.sex%type,
dept_id1 student2.dept_id%type)
as
begin
insert into student2(stuid,stuname,sex,dept_id) values(id1,name1,sex1,dept_id1);
dbms.output.put_line('stuid='||id1||':name1='||name1);
end;

begin
proc1(24,'龚大帅2','女',1);
end;
select * from student2;

--创建一个存储过程
create or replace procedure mypro
(
tid in varchar2
)
as
total number;
begin
select count(*) into total from emp;
end mypro;
select * from emp;
--调用存储过程
declare
name2 emp.ename%type;
job2 emp.job%type;
begin
mypro(7396,'',:job2);
print name2;
print job2;
end;

create or replace procedure mypro2
(tid in emp.empno%type,
 gname out emp.ename%type
)
as
begin
select ename into gname from emp where empno=tid;
end mypro2;
--调用
select * from emp;
select ename from emp where empno=7934;

--exec和begin的区别,exec中输出变量需要使用冒号
--begin中不需要使用我们的冒号,直接使用变量传入即可
--不过print和dbms输出不能乱用,只能配套使用哦
variable name1 varchar2(40);
exec mypro2(7934,:name1);
print name1;

set serveroutput on
declare
name1 varchar2(40);
begin
mypro2(7369,name1);
dbms_output.put_line(name1);
end;

--=======================================================================
--下面的都是函数的应用

--注意返回值类型,不需要定义变量,这个错误调了半天都没哟看出来
create or replace function fun1(var_deptno number)
return number
as
con number;
begin
select count(*) into con from emp where deptno=var_deptno;
return con;
end fun1;

select * from emp;

--调用函数
set serveroutput on
declare
name2 number;
begin
name2:=fun1(10);
dbms_output.put_line('num='||name2);
end;

--编写函数返回一条完整的信息7369的个人信息
create or replace function fun2(var_empno emp.empno%type)
return emp%rowtype
as
selfInfo emp%rowtype;
begin
select * into selfInfo from emp where empno=var_empno;
return selfinfo;
end fun2;
--调用该函数,输出返回的值
set serveroutput on
declare
selfInfo emp%rowtype;
begin
selfInfo:=fun2(7369);
dbms_output.put_line(selfInfo.ename|| selfinfo.sal|| selfinfo.job);
end;

--编写函数然后返回10号部门的素有的员工的信息
create or replace function fun3(var_empno number)
return  emp.ename%type
as
name1 emp.ename%type;
begin
select ename into name1 from emp where empno= var_empno;
return name1;
end fun3;
--调用该函数,然后输出返回的值
set serveroutput on
declare
name1 emp.ename%type;
begin
name1:=fun3(7698);
dbms_output.put_line('name1='||name1);
end;
--=======================================================================


==========================================================================
--三种循环的格式如下所示:
1:for循环格式如下所示:
for () in [reverse] 1..100 loop
  end loop;
2:while循环
  while () loop
  end loop;
3:do-while循环
  loop
  exit when ()
  end loop;
4:case语句
  case
  when () then ();
  when () then ();
  else ();
  end case;
--1+2+3+..+100求和
set serveroutput on
declare
sum1 int:=0;
begin
  --注意for循环中..默认就是自增1
  for i in 1..100 loop
    sum1:=sum1+i;
    dbms_output.put_line('i='||i);
  end loop;
  dbms_output.put_line('sum1='||sum1);
end;

--使用for倒叙输出1-10之间的数子
set serveroutput on
declare
begin
for i in reverse 1..10 loop
  dbms_output.put_line(i);
end loop;
end;

--使用while循环输出1-10之间的数字
set serveroutput on
declare
i int:=0;
begin
while i<10 loop
  i:=i+1;
  dbms_output.put_line(i);
end loop;
end;
--使用do-while循环输出1-10之间的数字
set serveroutput on
declare
i int:=0;
begin
loop
i:=i+1;
--dbms_output.put_line(i);
sop(i);
exit when(i>10);
end loop;
end;


--使用if语句进行判断等级
--注意了在sql中没有&&,||这样的符号
--这些符号都被and,or代替了
set serveroutput on
declare
score number:=185;
flag varchar2(10);--这里需要注意在定义变量的变量的时候需要加上长度,否则会报错
begin
if score<60 then
  flag:='1级';
elsif score<70 then
  flag:='2级';
elsif score>70 and score<90 then
  flag:='4级';
else
  flag:='5级';
end if;
--dbms_output.put_line('flag='||flag);
--可以使用存储过程进行输出
sop('flag='||flag);
end;


--使用case语句来输出等级制度
set serveroutput on
declare
score number:=18;
flag varchar(50);--这里需要加上长度来约束这个字符的长度,以免长处啦范围
begin
case when score<60 then flag:='1级';
  when score<70 then flag:='2级';
  when score<90 then flag:='3级';
  else flag:='4级';
  end case;
  sop('flag='||flag);
end;

--使用case语句来判断优秀还是及格
set serveroutput on
declare
score number:=89;
flag varchar2(30);--注意这里需要加上字节长度,否则他无法判断会不会超多字节长度
begin
case
when score<60 then flag:='不及格';
when score<70 then flag:='及格';
when score<80 then flag:='良好';
else flag:='优秀';
end case;
--输出返回的结果
sop('flag='||flag);
end;

--这个是用来输出字符串的输出过程
create or replace procedure sop(var varchar)
as
begin
dbms_output.put_line(var);
end sop;

==================================================================
--下面开始编写游标
sql%found
sql%notfound
sql%rowcount
sql%isopen

--先定义一个显示的游标
set serveroutput on
declare
name1 student2.stuname%type;
sex1 student2.sex%type;
cursor mycursor is
select stuname,sex from student2;
begin
--打开游标
open mycursor;
--循环取出数据
loop
fetch mycursor into name1,sex1;
dbms_output.put_line(name1||':'||sex1);
exit when mycursor%notfound;
end loop;
--关闭游标
close mycursor;
end;

--使用for循环,会更简单取出数据
set serveroutput on
declare
cursor mycursor2 is
select * from student2;
begin
for stuInfo in mycursor2 loop
  dbms_output.put_line(stuInfo.stuname||':'||stuInfo.sex);
end loop;
end;

desc student2;

select * from student2;
--编写一个游标,然后通过游标进行给每一个学生工资上涨10%
set serveroutput on
declare
sal1 student2.sal%type;
cursor updateCursor is
select * from student2;
begin
for stuInfo in updateCursor loop
  dbms_output.put_line('更新前的sal:'||stuInfo.sal);
  update student2 set sal=sal*1.1 where stuid=stuInfo.stuid;
  select sal into sal1 from student2  where stuid=stuInfo.stuid;
  dbms_output.put_line('更新后的sal:'||sal1);
end loop;
end;


create or replace procedure proWithCursor
as
cursor proCursor is
select * from student2;
begin
for stuInfo in proCursor loop
  dbms_output.put_line(stuInfo.stuname||':'||stuInfo.sex||':'||stuInfo.sal);
end loop;
end;
--调用该存储过程
set serveroutput on
declare
begin
proWithCursor();
end;

猜你喜欢

转载自blog.csdn.net/qq_35971258/article/details/81108218