Oracle PL / SQL-간단한 배열 변수를 만드는 방법은 무엇입니까?
PL / SQL 코드에서 사용할 수있는 메모리 내 배열 변수를 만들고 싶습니다. 순수 메모리를 사용하는 Oracle PL / SQL의 컬렉션을 찾을 수 없으며 모두 테이블과 관련이있는 것 같습니다. PL / SQL (C # 구문)에서 이와 같은 작업을 수행하려고합니다.
string[] arrayvalues = new string[3] {"Matt", "Joanne", "Robert"};
편집 : Oracle : 9i
고정 크기 배열에 VARRAY를 사용할 수 있습니다.
declare
type array_t is varray(3) of varchar2(10);
array array_t := array_t('Matt', 'Joanne', 'Robert');
begin
for i in 1..array.count loop
dbms_output.put_line(array(i));
end loop;
end;
무한 배열의 경우 TABLE :
...
type array_t is table of varchar2(10);
...
여기서 "테이블"이라는 단어는 혼란스럽게 데이터베이스 테이블과 관련이 없습니다. 두 방법 모두 메모리 내 배열을 만듭니다.
이 중 하나를 사용하면 요소를 추가하기 전에 컬렉션을 초기화하고 확장해야합니다.
declare
type array_t is varray(3) of varchar2(10);
array array_t := array_t(); -- Initialise it
begin
for i in 1..3 loop
array.extend(); -- Extend it
array(i) := 'x';
end loop;
end;
첫 번째 인덱스는 0이 아닌 1입니다.
BINARY_INTEGER에 의해 색인화 된 메모리 내 변수 길이 배열을 보유하도록 DBMS_SQL.VARCHAR2_TABLE을 선언 할 수 있습니다.
DECLARE
name_array dbms_sql.varchar2_table;
BEGIN
name_array(1) := 'Tim';
name_array(2) := 'Daisy';
name_array(3) := 'Mike';
name_array(4) := 'Marsha';
--
FOR i IN name_array.FIRST .. name_array.LAST
LOOP
-- Do something
END LOOP;
END;
You could use an associative array (used to be called PL/SQL tables) as they are an in-memory array.
DECLARE
TYPE employee_arraytype IS TABLE OF employee%ROWTYPE
INDEX BY PLS_INTEGER;
employee_array employee_arraytype;
BEGIN
SELECT *
BULK COLLECT INTO employee_array
FROM employee
WHERE department = 10;
--
FOR i IN employee_array.FIRST .. employee_array.LAST
LOOP
-- Do something
END LOOP;
END;
The associative array can hold any make up of record types.
Hope it helps, Ollie.
Another solution is to use an Oracle Collection as a Hashmap:
declare
-- create a type for your "Array" - it can be of any kind, record might be useful
type hash_map is table of varchar2(1000) index by varchar2(30);
my_hmap hash_map ;
-- i will be your iterator: it must be of the index's type
i varchar2(30);
begin
my_hmap('a') := 'apple';
my_hmap('b') := 'box';
my_hmap('c') := 'crow';
-- then how you use it:
dbms_output.put_line (my_hmap('c')) ;
-- or to loop on every element - it's a "collection"
i := my_hmap.FIRST;
while (i is not null) loop
dbms_output.put_line(my_hmap(i));
i := my_hmap.NEXT(i);
end loop;
end;
You can also use an oracle defined collection
DECLARE
arrayvalues sys.odcivarchar2list;
BEGIN
arrayvalues := sys.odcivarchar2list('Matt','Joanne','Robert');
FOR x IN ( SELECT m.column_value m_value
FROM table(arrayvalues) m )
LOOP
dbms_output.put_line (x.m_value||' is a good pal');
END LOOP;
END;
I would use in-memory array. But with the .COUNT
improvement suggested by uziberia:
DECLARE
TYPE t_people IS TABLE OF varchar2(10) INDEX BY PLS_INTEGER;
arrayvalues t_people;
BEGIN
SELECT *
BULK COLLECT INTO arrayvalues
FROM (select 'Matt' m_value from dual union all
select 'Joanne' from dual union all
select 'Robert' from dual
)
;
--
FOR i IN 1 .. arrayvalues.COUNT
LOOP
dbms_output.put_line(arrayvalues(i)||' is my friend');
END LOOP;
END;
Another solution would be to use a Hashmap like @Jchomel did here.
NB:
With Oracle 12c you can even query arrays directly now!
Sample programs as follows and provided on link also https://oracle-concepts-learning.blogspot.com/
plsql table or associated array.
DECLARE
TYPE salary IS TABLE OF NUMBER INDEX BY VARCHAR2(20);
salary_list salary;
name VARCHAR2(20);
BEGIN
-- adding elements to the table
salary_list('Rajnish') := 62000; salary_list('Minakshi') := 75000;
salary_list('Martin') := 100000; salary_list('James') := 78000;
-- printing the table name := salary_list.FIRST; WHILE name IS NOT null
LOOP
dbms_output.put_line ('Salary of ' || name || ' is ' ||
TO_CHAR(salary_list(name)));
name := salary_list.NEXT(name);
END LOOP;
END;
/
참고URL : https://stackoverflow.com/questions/7012625/oracle-pl-sql-how-to-create-a-simple-array-variable
'development' 카테고리의 다른 글
소프트 키보드가 레이아웃을 올리는 것을 피하는 방법은 무엇입니까? (0) | 2020.07.14 |
---|---|
Bash에서 두 개의 부동 소수점 숫자를 비교하는 방법은 무엇입니까? (0) | 2020.07.14 |
모든 0 값을 NA로 교체 (0) | 2020.07.14 |
목록에서 면도기 드롭 다운 목록 채우기 (0) | 2020.07.14 |
Github 푸시 오류 : RPC 실패; (0) | 2020.07.14 |