Oracle筆記 六、PL/SQL簡單語句塊、變數定義

來源:互聯網
上載者:User

標籤:

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簡單語句塊、變數定義

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.