PL/SQL练习基本的PL/SQL语句
Posted Tomatoes
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PL/SQL练习基本的PL/SQL语句相关的知识,希望对你有一定的参考价值。
1.无变量匿名快
begin
dbms_output.put_line(‘Hello World‘);
end;
2.有变量的匿名块,定义变量:
declare
v_ename varchar2(10);
v_sal number(7,2);
begin
select ename,sal into v_ename,v_sal from emp where empno=&n;
dbms_output.put_line(‘Employee name is: ‘||v_ename);
dbms_output.put_line(‘Employee sal is: ‘||v_sal);
end;
3.定义常量和变量并赋值:
declare
v_ename VARCHAR2(10);
v_sal NUMBER(7,2);
c_empno constant NUMBER(4):=7788; //声明一个常量
begin
select ename,sal into v_ename,v_sal from emp where empno=c_empno;
dbms_output.put_line(‘Employees name is: ‘||v_ename);
dbms_output.put_line(‘Employees salary is: ‘||v_sal);
end;
4.在执行部分给变量赋值:
declare
v_ename varchar2(10);
v_sal number(7,2);
v_empno number(4);
begin
v_empno :=7788;
select ename,sal into v_ename,v_sal from emp where empno=v_empno;
dbms_output.put_line(‘Employee name is: ‘||v_ename);
dbms_output.put_line(‘Employee sal is: ‘||v_sal);
end;
5.异常处理:
declare
v_ename varchar2(10);
v_sal number(7,2);
begin
select ename,sal into v_ename,v_sal from emp where empno=&n;
dbms_output.put_line(‘Employee name is: ‘||v_ename);
dbms_output.put_line(‘Employee sal is: ‘||v_sal);
exception
when no_data_found then
dbms_output.put_line(‘Please input current number!‘);
end;
6.通过%type,保证变量的数据类型和表中字段的数据类型一致:
declare
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
select ename,sal into v_ename,v_sal from emp where empno=&n;
dbms_output.put_line(‘Employee name is: ‘||v_ename);
dbms_output.put_line(‘Employee sal is: ‘||v_sal);
exception
when no_data_found then
dbms_output.put_line(‘Please input current number!‘);
end;
以上是关于PL/SQL练习基本的PL/SQL语句的主要内容,如果未能解决你的问题,请参考以下文章