plsql基本语法

语法

declare
    --说明部分(变量说明、游标声明、例外说明)
begin
    --语句序列(DML语句)
exception
    --例外处理语句
end;

常量和变量的定义

说明变量

char、varchar2、date、number、boolean、long,定义表使用的变量

name char(15);
--:=用来给变量赋值
married boolean := ture;

引用变量

--psal的类型与emp表中的sal列类型一样
declare
  --pename的类型与emp表中的ename列类型一样
  pename emp.ename%type;
  --pename的类型与emp表中的ename列类型一样
  psal emp.sal%type;
begin
  select ename,sal into pename,psal from emp where empno = 7839;
  dbms_output.put_line(pename||'工资是'||psal);
end;

记录型变量

declare
  --emp_rec变量表示emp中的一行
  emp_rec emp%rowtype;
begin
  select * into emp_rec from emp where empno = 7839;
  --使用变量名加.的方式来获取行中的列
  dbms_output.put_line(emp_rec.ename||'工资是'||emp_rec.sal);
end;

判断语句

语法

--语法1
if 条件 then 语句1;
语句2;
end if;

--语法2
if 条件 then 语句序列1;
else 语句序列2;
end if;

--语法3
if 条件 then 语句序列1;
elsif 条件 then 语句序列2;
else 语句序列3;
end if;

案例代码

declare
pnum number := #
begin
  if pnum = 0 then dbms_output.put_line('您输入的是0');
  elsif pnum = 1 then dbms_output.put_line('您输入的是1');
  elsif pnum = 2 then dbms_output.put_line('您输入的是2');
  else dbms_output.put_line('其他数字');
  end if;
end;

循环

语法

--while循环
while total <= 2500
loop
    ......
    total := total+salary;
end loop;


--loop循环
loop
    exit[ when 条件];
    ......
end loop;

--for循环
for 1 in 1..3
loop
    ......
end loop;

案例代码

--输出1到10的数字
declare
  pnum number := 1;
begin
  loop
    exit when pnum > 10;
    dbms_output.put_line(pnum);
    pnum := pnum + 1;
    end loop;
end;

 

posted @ 2018-07-30 17:48  风雪夜_归人  阅读(160)  评论(0编辑  收藏  举报