/*
plsql编程
declare
定义
变量名 变量类型;
变量名 变量类型 := 初始值;
vsal emp.sal%type;
变量名 表名.列名%type; 引用型变量,引用表列的变量类型
vrow emp%rowtype;
变量名 表名%rowtype; 引用表 一行的 变量类型
begin
执行语句
end;
dbms_output.put_line(); 输出语句
*/
-- 定义i =1;且输出
declare
i number := 1;
begin
dbms_output.put_line(i);
end;
-- 查询表格emp 工资 --800
select sal from emp where empno = 7369;
declare
vsal emp.sal%type;
begin
select sal into vsal from emp where empno = 7369;
dbms_output.put_line(vsal);
end;
-- 查询一行数据
select * from emp where empno = 7369;
declare
vrow emp%rowtype;
begin
select * into vrow from emp where empno = 7369;
dbms_output.put_line(vrow.empno);
dbms_output.put_line(vrow.ename);
dbms_output.put_line(vrow.job);
dbms_output.put_line(vrow.sal);
end;
/*
plsql中if条件判断
if then
elsif then
else
end id
*/
-- 根据不同年龄,输出不同值
declare
age number := 22;
begin
if age < 18 then
dbms_output.put_line('未成年');
elsif age > 18 and age < 40 then
dbms_output.put_line('成年');
else
dbms_output.put_line('老年');
end if;
end;
/*
plsql中循环
*/
-- while循环
/*
while循环
while 条件 loop
end loop;
*/
declare
i number :=1;
begin
while i < 10 loop
dbms_output.put_line(i);
i := i+1;
end loop;
end;
-- for循环
/*
for循环
for 条件 loop -- reverse 是倒叙。默认1-10,reverse 是从10-1;
end loop;
for i in [reverse] 1..10 loop
dbms_output.put_line(i);
end loop;
*/
declare
begin
for i in reverse 1..10 loop
dbms_output.put_line(i);
end loop;
end;
-- loop循环
/*
loop 循环
loop
exit 退出 when 条件
exit when i> 10; 当i> 10时退出循环
end loop;
*/
declare
i number :=1;
begin
loop
exit when i>10;
dbms_output.put_line(i);
i := i+1;
end loop;
end;
-- 输出一个5行5列正方形
/*
1 1
2 3
3 5
1 3 5-2*i
2 1
*/
declare
begin
for i in 1..5 loop
for j in 1..5 loop
dbms_output.put('*');
end loop;
dbms_output.put_line('');
end loop;
end;
-- 输出菱形
/*
看成上下
上三行,下2行
*/
declare
begin
-- 循环上三次
for i in 1..3 loop
-- 输出空格
for k in 1..3-i loop
dbms_output.put(' ');
end loop;
-- 输出前三行*
for j in 1..2*i-1 loop
dbms_output.put('*');
end loop;
dbms_output.put_line('');
end loop;
-- 循环下俩行
for i in 1..2 loop
-- 输出空格
for k in 1..i loop
dbms_output.put(' ');
end loop;
-- 输出 下俩行*
for j in 1..5-2*i loop
dbms_output.put('*');
end loop;
dbms_output.put_line('');
end loop;
end;