例1: SQL>set serveroutput on --打开输出选项
begin
dbms_output.putline(‘Hello PL/SQL !‘);
end; 例2 SQL>declare
v_ename varchar2(5); --定义一个字符串变量
begin
select ename into v_ename from emp where empno = &no;
dbms_output.put_line(‘姓名:‘ || v_ename);
end;
说明:& 表示要从控制台输入的变量
例3 SQL>declare v_ename varchar2(5); --定义一个字符串变量
begin
select ename into v_ename from emp where empno = &no;
dbms_output.put_line(‘姓名:‘ || v_ename);
--下面是异常处理
exception
when no_data_found then
dbms_output.put_line(‘出现错误!‘);
end;
注意:no_data_found 表示一种异常,即如果用户输入的参数"&no" 查不出数据
7 创建存储过程 SQL>create procedure sp_proName1(spName varchar2,newSalary number) is
begin
update emp set sal =newSalary where ename = spName
end;
16 if语句的使用 SQL>create or replace procedure Name1(spName varchar2) is
v_salary emp.sal%type;
begin
select sal into v_salary from emp where ename = spName;
if v_salary < 3000 then
update emp set sal=sal * 1.2 where ename = spName;
end if;
end;
17 if...then...else语句 SQL> create or replace procedure Name1(spName varchar2) is
v_salary emp.sal%type;
begin
select sal into v_salary from emp where ename = spName;
if v_salary < 3000 then
update emp set sal=sal * 1.2 where ename = spName;
else
update emp set sal=sal * 1.05 where ename = spName;
end if;
end;
18 使用loop循环 SQL>create or replace procedure Name1(spName varchar2) is
v_num :=1;
begin
loop
insert into users values (v_num,spname);
exit whrn v_num=10;
v_num := v_num + 1;
end loop;
end; 注意:在这里“:=”表示赋值的意思
19 使用 while 循环 SQL>create or replace procedure Name1(spName varchar2) is
v_num :=1;
begin
while v_num <10 loop
insert into users values (v_num,spname);
v_num := v_num + 1;
end loop;
end;