oracle變量

--1定義標量變量
--
(1)定義語法
變量名 [constant] 數據類型 [not null] [:= | default expr]
-- constant:用於指定常量。必須指定初始值
--
 := 用於爲變量和常量指定初始值
--
 expr初始值的pl/sql表達式,可以是文本值、變量、函數等
--
示例1:
v_name varchar2(10);
v_rate constant 
number(3,2) := 5.5;
v_valid boolean 
not null default false;
--(2)使用
--
變量賦值使用等號前加冒號(:=)
--
示例2:
declare
v_name 
varchar2(10);
v_money 
number(6,2);
c_tax_rate constant 
number(3,2) := -0.03;
v_tax_money 
number(6,2);
begin
 
select user_name,game_money into v_name,v_money 
 
from user_info where user_id = 100000;
 v_tax_money :
= v_money*c_tax_rate;
 dbms_output.put_line(
'name:'||v_name);
 dbms_output.put_line(
'money:'||v_money);
 dbms_output.put_line(
'rate:'||v_tax_money);
end
--(3)使用%TYPE屬性
v_name user_info.user_name%TYPE;
v_money user_info.game_money
%TYPE;
c_tax_rate constant 
number(3,2) := -0.03;
v_tax_money v_money
%TYPE;
--變量v_name,v_money與user_info表的user_name,game_money列的類型和長度完全一致
--
變量v_tax_money與變是v_money的類型和長度完全一致

--2複合變量
--
(1)pl/sql記錄(類似於結構)
--
 在定義部分定義記錄類型和記錄變量,在執行部分引用該記錄變量
--
 引用記錄成員時必須要加記錄變量作爲前綴(記錄變量。記錄成員)
--
示例3:
declare
TYPE record_type 
IS RECORD(
  v_name user_info.
user_name%TYPE,
  v_money user_info.game_money
%TYPE
);
emp_record record_type;
begin
 
select user_name,game_money into emp_record 
 
from user_info where user_id = 100000;
 dbms_output.put_line(
'name:'||emp_record.v_name);
 dbms_output.put_line(
'money:'||emp_record.v_money);
end
--(2)pl/sql表(類似於數組)
--
 pl/sql表與數組區別:下標沒有上下限,個數年沒有限制,下票可以爲負值
--
 必須先在定義部分定義pl/sql表類型和pl/sql表變量,在執行部分引用該pl/sql表變量
--
示例4:
declare
TYPE name_table_type 
IS TABLE OF user_info.user_name%TYPE 
  
INDEX BY BINARY_INTEGER;
v_name name_table_type;
begin
 
select user_name into v_name(-1
 
from user_info where user_id = 100000;
 dbms_output.put_line(
'name:'||v_name(-1));
end
--(3)嵌套表
--
(4)VARRAY(變長數組)

--3 參照變量
--
 用於存放數值指針的變量。
--
(1)遊標變量(REF CURSOR)
--
 靜態遊標:需要在定義遊標時指定相應的select語句
--
 示例5:
declare
  type c1 
is ref cursor;--c1爲ref cursor類型
  emp_cursor c1;--emp_cursor爲遊標變量
  v_name user_info.user_name%TYPE;
  v_money user_info.game_money
%TYPE;
begin
  
open emp_cursor for --打開遊標變量時指定了對應的select語句
  select user_name,game_money from user_info where  user_id = 100000;
  loop
    
fetch emp_cursor into v_name,v_money;
    
exit when emp_cursor%notfound;
    dbms_output.put_line(v_name);
  
end loop;
end;
-- 動態遊標:在定義遊標變量時不要需指定相應的select語句,而是打開遊標時指定select語句
--
(2)對象類型變量(REF obj_type)
--
 是指向對象實例的指針
--
 示例6:
create or replace type home_type as object(--建立對象類型
  street varchar2(50),city varchar2(20),
  state 
varchar2(20),zipcode varchar2(6),
  owner 
varchar2(10)
);
create table homes of home_type;--建表
insert into homes values('上海路100號','上海','200000','junly');
commit;
--對象表homes存放家庭地址及戶主姓名,如每個家庭有四口人,爲了同一家庭成員共享家庭地址,
--
可使用REF引用home_type對象類型,從而降低佔用空間。
create table person(
  id 
number(6primary key,
  name 
varchar2(10),
  addr ref home_type
);
insert into person select 1,'junly',ref(p) from homes p where p.owner='junly'
insert into person select 2,'junl2',ref(p) from homes p where p.owner='junly'
--person表插入數據時,addr列將存入指向homes表相應數據的地址指針

--4 LOB變量
/**//*用於存儲大批量數據的變量
(1)內部LOB
 CLOB    支技事務操作   存儲數據庫中   用於存儲大批量字符數據
 BLOB    支技事務操作   存儲數據庫中   用於存儲大批量二進制數據
 NCLOB   支技事務操作   存儲數據庫中   用於存儲大批量字符數據
(2)外部LOB
 BFILE   不支技事務     存在OS文件中   存儲指向OS文件的指針
*/


--5非PL/SQL變量
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章