新聞中心
oracle怎么使用數(shù)組
除了構(gòu)造函數(shù)外,集合還有很多內(nèi)建函數(shù),這些函數(shù)稱為方法。 調(diào)用方法的語法如下: collectionmethod 下表中列出oracle中集合的方法 方法 描述 使用限制 COUNT 返回集合中元素的個數(shù) DELETE 刪除集合中所有元素 DELETE() 刪除元素下標為x的元素oracle怎么使用數(shù)組
祁連ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
如何在oracle建一個存儲過程來遍歷數(shù)組,新手求解
SQL code
DECLARE
-- Define a varray of twelve strings.
TYPE months_varray IS VARRAY(12) OF STRING(9 CHAR);
-- Define an associative array of strings.
TYPE calendar_table IS TABLE OF VARCHAR2(9 CHAR)
INDEX BY BINARY_INTEGER;
-- Declare and construct a varray.
month MONTHS_VARRAY :=
months_varray('January','February','March'
,'April','May','June'
,'July','August','September'
,'October','November','December');
-- Declare an associative array variable.
calendar CALENDAR_TABLE;
BEGIN
-- Check if calendar has no elements.
IF calendar.COUNT = 0 THEN
-- Print a title
DBMS_OUTPUT.PUT_LINE('Assignment loop:');
DBMS_OUTPUT.PUT_LINE('----------------');
-- Loop through all the varray elements.
FOR i IN month.FIRST..month.LAST LOOP
-- Initialize a null associative array element.
calendar(i) := '';
-- Print an indexed element from the associative array.
DBMS_OUTPUT.PUT_LINE(
'Index ['||i||'] is ['||calendar(i)||']');
-- Assign the numeric index valued varray element
-- to an equal index valued associative array element.
calendar(i) := month(i);
END LOOP;
-- Print a title
DBMS_OUTPUT.PUT(CHR(10));
DBMS_OUTPUT.PUT_LINE('Post-assignment loop:');
DBMS_OUTPUT.PUT_LINE('---------------------');
-- Loop through all the associative array elements.
FOR i IN calendar.FIRST..calendar.LAST LOOP
-- Print an indexed element from the associative array.
DBMS_OUTPUT.PUT_LINE(
'Index ['||i||'] is ['||calendar(i)||']');
END LOOP;
END IF;
END;
Oracle PL/SQL (5) - 可變數(shù)組IS VARRAY(長度) OF的使用
可變數(shù)組,是一種集合。一個可變數(shù)組是一個對象的集合,其中每個對象都具有相同的數(shù)據(jù)類型。可變數(shù)組的大小在創(chuàng)建時決定。在表中創(chuàng)建可變數(shù)組后,可變數(shù)組在主表中即為一個列。
可變數(shù)組特性
1)可變數(shù)組主要的特性即是元素的最大個數(shù)是有限制
2)可變數(shù)組下標固定為1,上限可以擴展
3)在可變數(shù)組聲明時自動設置為NULL值.所謂的空值指的是集合本身是空,不是針對它所擁有的元素,故在元素引用前需要對其進行初始化
可變數(shù)組創(chuàng)建語法:
CREATE [OR REPLACE] TYPE 類型名稱 IS VARRAY(長度) OF 數(shù)據(jù)類型;
例如:定義一個可變數(shù)組類型 my_varray ,它的最大容量是5,元素類型是 VARCHAR2.
1、簡單類型的可變數(shù)組
2、定義TYPE類型的可變數(shù)組
輸出結(jié)果:公司code:204
3、定義二維可變數(shù)組
輸出結(jié)果:
4、定義復合類型的可變數(shù)組
輸出結(jié)果:
事故號:AAA 版本號:1 有效標識:1
事故號:BBB 版本號:2 有效標識:1
事故號:CCC 版本號:3 有效標識:0
如何在ORACLE中定義一個數(shù)組
集合:是具有相同定義的元素的聚合。Oracle有兩種類型的集合:
可變長數(shù)組(VARRAY):可以有任意數(shù)量的元素,但必須預先定義限制值。
嵌套表:視為表中之表,可以有任意數(shù)量的元素,不需要預先定義限制值。
在PL/SQL中是沒有數(shù)組(Array)概念的。但是如果程序員想用Array的話,就得變通一下,用TYPE
和Table
of
Record來代替多維數(shù)組,一樣挺好用的。
emp_type
就好象一個table
中的一條record
一樣,里面有id,
name,gender等。emp_type_array
象個table,
里面含有一條條這樣的record
(emp_type),就象多維數(shù)組一樣。
--單維數(shù)組
DECLARE
TYPE
emp_ssn_array
IS
TABLE
OF
NUMBER
INDEX
BY
BINARY_INTEGER;
best_employees
emp_ssn_array;
worst_employees
emp_ssn_array;
BEGIN
best_employees(1)
:=
'123456';
best_employees(2)
:=
'888888';
worst_employees(1)
:=
'222222';
worst_employees(2)
:=
'666666';
FOR
i
IN
1..best_employees.count
LOOP
DBMS_OUTPUT.PUT_LINE('i='||
i
||
',
best_employees=
'
||best_employees(i)
||
',
worst_employees=
'
||worst_employees(i));
END
LOOP;
END;
--多維數(shù)組
DECLARE
TYPE
emp_type
IS
RECORD
(
emp_id
employee_table.emp_id%TYPE,
emp_name
employee_table.emp_name%TYPE,
emp_gender
employee_table.emp_gender%TYPE
);
TYPE
emp_type_array
IS
TABLE
OF
emp_type
INDEX
BY
BINARY_INTEGER;
emp_rec_array
emp_type_array;
emp_rec
emp_type;
BEGIN
emp_rec.emp_id
:=
300000000;
emp_rec.emp_name
:=
'Barbara';
emp_rec.emp_gender
:=
'Female';
emp_rec_array(1)
:=
emp_rec;
emp_rec.emp_id
:=
300000008;
emp_rec.emp_name
:=
'Rick';
emp_rec.emp_gender
:=
'Male';
emp_rec_array(2)
:=
emp_rec;
FOR
i
IN
1..emp_rec_array.count
LOOP
DBMS_OUTPUT.PUT_LINE('i='||i
||',
emp_id
='||emp_rec_array(i).emp_id
||',
emp_name
='||emp_rec_array(i).emp_name
||',
emp_gender
=
'||emp_rec_array(i).emp_gender);
END
LOOP;
END;
--------------
Result
--------------
i=1,
emp_id
=300000000,
emp_name
=Barbara,
emp_gender
=
Female
i=2,
emp_id
=300000008,
emp_name
=Rick,
emp_gender
=
Male
當前題目:oracle怎么創(chuàng)建數(shù)組,oracle中數(shù)組使用
分享路徑:http://www.dlmjj.cn/article/hdeiii.html