zoukankan      html  css  js  c++  java
  • Oracle笔记 六、PL/SQL简单语句块、变量定义

    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;
  • 作者:hoojo
    出处:
    blog:http://blog.csdn.net/IBM_hoojo
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权所有,转载请注明出处 本文出自:
分享道版权所有,欢迎转载,转载请注明出处,谢谢
收藏
关注
评论
查看全文
  • 相关阅读:
    ADO.NET访问Access2007的数据库 IErrorInfo.GetDescription failed with E_FAIL(0x80004005)
    Git工程实践
    来用java类模拟tensorflow工作流程
    Java异常处理之throw和声明throws·12
    Java异常之finally和多重捕获·11
    linux和windows如何查询自己本地出去的公网ip地址
    CentOS 6 EOL切换源
    P4782 【模板】2-SAT 问题
    P3834 【模板】可持久化线段树 2(主席树)
    P2671 [NOIP2015 普及组] 求和
  • 原文地址:https://www.cnblogs.com/hoojo/p/2035335.html
  • Copyright © 2011-2022 走看看