码迷,mamicode.com
首页 > 数据库 > 详细

【PL/SQL练习】基本的PL/SQL语句

时间:2016-11-26 17:27:09      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:code   lease   log   通过   please   sql语句   employees   out   rom   

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语句

标签:code   lease   log   通过   please   sql语句   employees   out   rom   

原文地址:http://www.cnblogs.com/tomatoes-/p/6104213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!