PL/SQL 11g R2 —— 定义PL/SQL变量 !

PL/SQL 11g R2 —— 定义PL/SQL变量 !_第1张图片

identifier [constant] datatype [not null] [:= | default expr];
v_ename varchar2(10);
v_sal number(6,2);
v_tax constant number(3,2) := 5.5;
v_data data;v_b boolean not null default false; 


例子1:

SQL> set serveroutput on
SQL> declare
  2    v_myname varchar2(20);
  3  begin
  4    dbms_output.put_line('My name is :' || v_myname);
  5    v_myname := 'zhangshenghong';
  6    dbms_output.put_line('My name is :' || v_myname);
  7  end;
  8  /
My name is :
My name is :zhangshenghong

PL/SQL 过程已成功完成。

例子2:

SQL> set serveroutput on
SQL> declare
  2    v_myname varchar2(20) default 'zhangqu';
  3  begin
  4    v_myname := 'zhangshenghong';
  5    dbms_output.put_line('My name is :' || v_myname);
  6  end;
  7  /
My name is :zhangshenghong

PL/SQL 过程已成功完成。

例子3:

SQL> set serveroutput on
SQL> declare
  2    v_event varchar2(20) := 'Wather''s day';
  3  begin
  4    dbms_output.put_line(v_event);
  5    v_event := q'!Father's day!';
  6    dbms_output.put_line(v_event);
  7    v_event := q'[Monther's day]';
  8    dbms_output.put_line(v_event);
  9  end;
 10  /
Wather's day
Father's day
Monther's day

PL/SQL 过程已成功完成。



PL/SQL 11g R2 —— 定义PL/SQL变量 !_第2张图片


绑定变量用法:

SQL> set serveroutput on
SQL> variable b_salary number;
SQL> begin
  2    select salary into :b_salary from employees where employee_id = 178;
  3  end;
  4  /

PL/SQL 过程已成功完成。

SQL> print b_salary;

  B_SALARY
----------
      7000

SQL> select first_name,last_name from employees where salary = :b_salary;

FIRST_NAME           LAST_NAME
-------------------- -------------------------
Oliver               Tuvault
Sarath               Sewall
Kimberely            Grant

你可能感兴趣的:(PL/SQL 11g R2 —— 定义PL/SQL变量 !)