--PL/SQL编程(procedure language )过程化的SQL编程
--SQL块,过程procedure,函数function,包package,触发器trigger
--简单块
顺序 赋值语句 :=赋值号 =C语言 :=PASCAL
有关绑定变量 &绑定变量名
--简单块
declare
v_sal emp.sal%type;
begin
select sal into v_sal from emp where ename=ⅆ
dbms_output.put_line('salary is'||v_sal);
exception
when no_data_found then
dbms_output.put_line('输入错误!');
end;
--块体中的例外
--变量v_
--常量c_
--游标变量(emp)_cursor(指针类型)
declare
v_numA number(3);
begin
v_numA:=&no;
dbms_output.put_line('numA is'||v_numa);
end;
分支
循环
块
过程
函数
包
PL/SQL块
declare
声明部分(v_sal emp.sal%type)
begin
块体
end;
dbms_output(输出包)put_line(过程)
字符串连接||
--简单块:问候
declare
begin
dbms_output.put_line('HELLO');
end;
--取出SCOTT的工资,显示输出姓名和工资
declare
v_sal emp.sal%type;
v_name emp.ename%type;
v_job emp.job%type;
begin
v_name:='SCOTT';
select sal,job into v_sal,v_job from emp where ename=v_name;
dbms_output.put_line('姓名是'||v_name||' 的工资是'||v_sal||'职务是'||v_job);
end;
--简单的PL/SQL过程
create or replace procedure emp_pro1
is
v_sal emp.sal%type;
v_name emp.ename%type;
v_job emp.job%type;
begin
v_name:='SCOTT';
select sal,job into v_sal,v_job from emp where ename=v_name;
dbms_output.put_line('姓名是'||v_name||' 的工资是'||v_sal||'职务是'||v_job);
end;
--过程的调用
exec 过程名;
declare
声明部分
begin
块体(正常,例外)
exception
end;
--25/10月
一、记录类型
二、行记录类型
三、绑定变量
四、例外
declare
empinfo emp%rowtype;
begin
select * into empinfo from emp where ename=&aa;
dbms_output.put_line('num is '||empinfo.empno||'deptno is '||empinfo.deptno||'salary is '||empinfo.sal);
end;
--EMP中sal<2000,给sal增加10%
--sal>2000同时sal<3000,给sal增加5%
--其他情况sal不变
create or replace procedure emp_sal(cname varchar2)
as
v_sal emp.sal%type;
begin
select sal into v_sal from emp where ename=cname;
if v_sal<2000 then update emp set sal=sal*1.1 where ename=cname;
elsif v_sal<3000
then update emp set sal=sal*1.05 where ename=cname;
end if;
exception
when no_data_found then
dbms_output.put_line('查无此人');
end;
--emp表中comm为空,增加200,否则增加100
declare
v_comm emp.comm%type;
begin
select comm into v_comm from emp where empno=7788;
if v_comm=null then update emp set comm=comm+200 where empno=7788;
else update emp set comm=comm+100 where empno=7788;
end if;
end;
--问题:编写一个过程,可以输入一个雇员编号,
--如果该雇员的职位是PRESIDENT就给他的工资增加1000,
--如果该雇员的职位是MANAGER就给他的工资增加500,
--其它职位的雇员工资增加200。
create or replace procedure job_emp(eno varchar2)
is
v_job emp.job%type;
begin
select job into v_job from emp where empno=eno;
if v_job='PRESIDENT' then update emp set sal=sal+1000 where empno=eno;
elsif v_job='MANAGER' then update emp set sal=sal+500 where empno=eno;
else update emp set sal=sal+200 where empno=eno;
end if;
exception
when no_data_found then
dbms_output.put_line('输入雇员编码有误!');
end;
create or replace procedure zxx_pro1(zxxNo number)
as
v_job emp.job%type;
begin
select job into v_job from emp where empno=zxxno;
if v_job='PRESIDENT' then update emp set sal=sal+1000 where empno=zxxno;
elsif v_job='MANAGER' then update emp set sal=sal+500 where empno=zxxno;
else update emp set sal=sal+200 where empno=zxxno;
end if;
end;
--简单块
declare
v_ename emp.ename%type;
begin
select ename into v_ename from emp where empno=7788;
dbms_output.put_line('姓名是'||v_ename);
end;
顺序结构, := <> != ** and or not null
分支结构, 不区分大小写v_a
循环结构
默认规则
变量标识符v_
常量标识符c_ c_week BASIC语言 PASCAL语言
游标_cursor _cursor
例外e_ e_error
--简单块
declare
v_numA number(3);
v_numb number(3);
begin
v_numa:=10;
v_numb:=20;
v_numa:=v_numa+v_numb;
dbms_output.put_line('两数之和='||v_numa);
end;
--简单块
declare
v_eno emp.empno%type;
v_sal emp.sal%type;
begin
select empno,sal into v_eno, v_sal from emp where ename='SCOTT';
dbms_output.put_line('编码是'||v_eno||' 工资是'||v_sal);
end;
--记录类型
declare
type emp_type_record is record
(
v_ename emp.ename%type,
v_job emp.job%type,
v_sal emp.sal%type
);
empinfo emp_type_record;
begin
select ename,job,sal into empinfo from emp where empno=7788;
dbms_output.put_line('雇员'||empinfo.v_ename||'的职务是'||empinfo.v_job||',工资是'||empinfo.v_sal);
end;
--%ROWTYPE类型:工资低于2000,提高10%,工资2000--3000,提高5%,高于3000,不做处理。
declare
v_emp emp%rowtype;
begin
select * into v_emp from emp where empno=7788;
if v_emp.sal<2000 then update emp set sal=sal*1.1 where empno=7788;
elsif v_emp.sal<3000
then update emp set sal=sal*1.05 where empno=&no;
end if;
--dbms_output.put_line('雇员'||v_emp.ename||'的编号是'||v_emp.empno||'职务"||v_emp.job||' his salgrade is'||v_emp.sal);
end;
备份地址: 【Oracle笔记(二)】