我有一个配置表,如果record_tm超过某个可配置的时间(config_recordtrim_hrs),我想删除一行。换句话说,变量recordtrim_config来自另一个表,其中有人写一个值(例如24小时24),之后将删除一条记录。
DECLARE
recordtrim_flag configs_table.value%type;
recordtrim_config configs_table.value%type;
recordtrim_interval interval;
BEGIN
SELECT configs_table.value INTO recordtrim_flag FROM configs_table WHERE configs_table.name = 'enable_recordtrim';
SELECT configs_table.value INTO recordtrim_config FROM configs_table WHERE configs_table.name = 'config_recordtrim_hrs';
SELECT CAST (recordtrim_config AS interval) INTO recordtrim_interval;
IF (recordtrim_flag = 'T') THEN
DELETE FROM main_records WHERE record_tm + recordtrim_interval < current_timestamp;
END IF;
END;
此代码在我尝试将recordtrim_config作为间隔进行转换的行中引发错误。我该如何解决这个问题?
编辑:我的错误:
错误报告:ORA-06550:第8行,第40列:PL / SQL:ORA-30089:缺失 或无效ORA-06550:第8行,第1列:PL / SQL:SQL 声明被忽略
编辑2:新代码:
DECLARE
recordtrim_flag configs_table.value%type;
recordtrim_config configs_table.value%type;
recordtrim_interval interval;
BEGIN
SELECT configs_table.value INTO recordtrim_flag FROM configs_table WHERE configs_table.name = 'enable_recordtrim';
SELECT configs_table.value INTO recordtrim_config FROM configs_table WHERE configs_table.name = 'config_recordtrim_hrs';
recordtrim_interval := NUMTODSINTERVAL (recordtrim_config, 'HOUR');
IF (recordtrim_flag = 'T') THEN
DELETE FROM main_records WHERE record_tm + recordtrim_interval < current_timestamp;
END IF;
END;
第11行的新错误,recordtrim_interval是无效的标识符
答案 0 :(得分:1)
错误的直接原因是您只有interval
而不是类型,即interval day to second
。你还必须select
来自某些东西 - 这里可以是from dual
,虽然直接分配更清晰。
如果是单个数字,则无法直接投射。相反,您可以使用numtodsinterval
function:
recordtrim_interval := NUMTODSINTERVAL (recordtrim_config, 'HOUR');
e.g。
set serveroutput on
declare
recordtrim_config varchar2(4) := '24';
recordtrim_interval interval day to second;
begin
recordtrim_interval := NUMTODSINTERVAL (recordtrim_config, 'HOUR');
dbms_output.put_line(recordtrim_interval);
end;
/
anonymous block completed
+01 00:00:00.000000
或直接从表中选择该变量。
如果您需要更复杂的值,而不是一个代表数小时的简单值 - 如果是这种情况,您将其存储为我认为的数字 - 那么如果您以合适的格式存储它,则可以施放它,或使用to_dsinterval
function代替;其中任何一个都会产生相同的结果:
declare
recordtrim_config varchar2(10) := '0 2:30:00';
recordtrim_interval interval day to second;
begin
recordtrim_interval := TO_DSINTERVAL (recordtrim_config);
dbms_output.put_line(recordtrim_interval);
SELECT CAST (recordtrim_config AS interval day to second)
INTO recordtrim_interval from dual;
dbms_output.put_line(recordtrim_interval);
recordtrim_interval := CAST (recordtrim_config AS interval day to second);
dbms_output.put_line(recordtrim_interval);
end;
/
anonymous block completed
+00 02:30:00.000000
+00 02:30:00.000000
+00 02:30:00.000000
在您编辑的问题中,ORA-00904: "RECORDTRIM_INTERVAL": invalid identifier
只是列表中的最后一个错误。回头看看,你会看到:
ORA-06550: line 4, column 21:
PLS-00201: identifier 'INTERVAL' must be declared
...其次是您尝试使用该变量的其他一些错误。您对变量的声明必须是:
recordtrim_interval interval day to second;
没有“interval
”数据类型; they are interval year to month
and interval day to second