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

Oracle笔记 六、PL/SQL简单语句块、变量定义

时间:2015-10-13 00:18:05      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:

1、简单SQL语句,HellWorld示例
--输出信息
begin
  dbms_output.put_line(‘Oracle Hello World!‘);
end;
 
2、变量的定义、使用
--定义变量
declare
  sName varchar2(20);
begin
  sName := ‘jack‘;
  dbms_output.put_line(sName);
end;
  
--常用类型
declare
    sNum number(1);
    sCount binary_integer := 0;
    sSal number(7, 2) := 5000.00;
    sDate date := sysdate;
    sPI number(3, 2) := 3.14;
    sValid boolean := true;
    sName varchar2(20) := ‘Jackson‘;
begin
    dbms_output.put_line(‘sName:‘ || sName);  
    dbms_output.put_line(‘sCount:‘ || sCount);  
    dbms_output.put_line(‘sSal:‘ || sSal);
    dbms_output.put_line(‘sDate:‘ || sDate);
    dbms_output.put_line(‘sPI:‘ || sPI);
    --dbms_output.put_line(‘sValid:‘ || sValid);
    dbms_output.put_line(‘sName:‘ || sName);
end;
 
--定义Table变量类型
declare 
  type type_table_emp_empno is table of emp.empno%type index by binary_integer;
  empnos type_table_emp_empno;
begin
  empnos(0) := 7369;
  empnos(2) := 6789;
  empnos(-1) := 6543;
  dbms_output.put_line(empnos(-1));
end;
 
--定义record变量类型
declare
  type type_record_dept is record (
       deptno dept.deptno%type,
       dname dept.dname%type,
       loc dept.loc%type
  );
  temp type_record_dept;
begin
  temp.deptno := 56;
  temp.dname := ‘software‘;
  temp.loc := ‘gz‘;
  dbms_output.put_line(temp.deptno || ‘ ‘ || temp.dname  || ‘ ‘ || temp.loc);
end;
 
--使用rowtype声明record变量
declare
  temp dept%rowtype;
begin
  temp.deptno := 57;
  temp.dname := ‘it‘;
  temp.loc := ‘sz‘;
  dbms_output.put_line(temp.deptno || ‘ ‘ || temp.dname  || ‘ ‘ || temp.loc);
end;
 
--sql语句完成变量赋值
declare
  v$sal emp.sal%type;
  v$ename emp.ename%type;
begin
  select sal, ename into v$sal, v$ename from emp where rownum = 1;
  dbms_output.put_line(v$sal || ‘ ‘ || v$ename);
end;
 
--sql语句完成rowtype变量赋值
declare
  v_row_emp emp%rowtype;
begin
  select * into v_row_emp from emp where empno = 7698;
  dbms_output.put_line(v_row_emp.sal || ‘ ‘ || v_row_emp.ename);
end;
 
--sql语句完成变量插入数据
create table dept2 as select * from dept;
declare
   deptno dept.deptno%type := 57;
   dname dept.dname%type := ‘software‘;
   loc dept.loc%type := ‘gz‘;
begin
    insert into dept2 values(deptno, dname, loc);
      commit;
end;
select * from dept2;

Oracle笔记 六、PL/SQL简单语句块、变量定义

标签:

原文地址:http://www.cnblogs.com/bokxy/p/4873025.html

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