簡體   English   中英

如何在Oracle中正確定義日期類型的數組?

[英]How to correctly define an array of date type in Oracle?

我想將字符串數組轉換為日期類型數組。 但是我不確定我的日期數組是否正確定義。 找不到有關如何定義或創建日期類型數組的任何示例。

這就是我聲明我的字符串數組和日期類型數組的方式。

create or replace TYPE array_collection IS table OF VARCHAR2(50) 
--string array is working absolutely fine so dw about that

create or replace type date_array is table of date;
--here i don't if I've defined this array correctly

我的轉換數組過程:

create or replace procedure convert_arr(
dat_ar in array_collection,perdate_arr out date_array)
as
begin
perdate_arr:=new date_array();
perdate_arr.extend(dat_ar.count);
for i in 1..dat_ar.count loop
perdate_arr(i):=to_date(dat_ar(i), 'yyyy-mm-dd');
end loop;
end convert_arr;
--compiles perfectly

調用匿名塊:

set serveroutput on
declare 
--l_dat array_collection;
--l_darray date_array;
l_dat array_collection:=array_collection();
l_darray date_array:=date_array();
begin
l_dat := array_collection('2011-01-01','2011-04-01','2011-05-01');
--l_dat.extend(3);
-- l_dat(1):= to_date(2019-07-08);
-- l_dat(2):= to_date(2019-07-09);
-- l_dat(3):= to_date(2019-06-02);

convert_arr(dat_ar=>l_dat,perdate_arr=>l_darray);
dbms_output.put_line('Number of array:' || l_dat.count);
for i in 1..l_dat.count loop
dbms_output.put_line('Date ' || i || ':' || to_char(l_dat(i),'dd/mm/yyyy'));
end loop;
end;

這個塊給我一個錯誤:

Error report -
ORA-01861: literal does not match format string
ORA-06512: at line 9
01861. 00000 -  "literal does not match format string"
*Cause:    Literals in the input must be the same length as literals in
           the format string (with the exception of leading whitespace).  If the
           "FX" modifier has been toggled on, the literal must match exactly,
           with no extra whitespace.
*Action:   Correct the format string to match the literal.

嘗試更改格式,但無濟於事。 任何幫助將不勝感激。 謝謝

由於l_darray是您的日期數組,因此請循環遍歷以顯示而不是l_dat

set serveroutput on
declare 
--l_dat array_collection;
--l_darray date_array;
l_dat array_collection:=array_collection();
l_darray date_array:=date_array();
begin
l_dat := array_collection('2011-01-01','2011-04-01','2011-05-01');
--l_dat.extend(3);
-- l_dat(1):= to_date(2019-07-08);
-- l_dat(2):= to_date(2019-07-09);
-- l_dat(3):= to_date(2019-06-02);

convert_arr(dat_ar=>l_dat,perdate_arr=>l_darray);
dbms_output.put_line('Number of array:' || l_darray.count);
for i in 1..l_darray.count loop
dbms_output.put_line('Date ' || i || ':' || to_char(l_darray(i),'dd/mm/yyyy'));
end loop;
end;
/

結果

Number of array:3
Date 1:01/01/2011
Date 2:01/04/2011
Date 3:01/05/2011


PL/SQL procedure successfully completed.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM