如何让pl/sql代码跳过多列中的空值
我目前正在编写一个从游标获取数据的PL/SQL脚本。游标是由多个分组集分组的,因此我在选择的几个条目中为多个列返回空值。在insert语句期间,有没有办法让pl/sql脚本跳过这些条目?我尝试过使用IF语句,但不起作用。下面是我的代码:提前谢谢如何让pl/sql代码跳过多列中的空值,sql,oracle,plsql,Sql,Oracle,Plsql,我目前正在编写一个从游标获取数据的PL/SQL脚本。游标是由多个分组集分组的,因此我在选择的几个条目中为多个列返回空值。在insert语句期间,有没有办法让pl/sql脚本跳过这些条目?我尝试过使用IF语句,但不起作用。下面是我的代码:提前谢谢 create or replace Procedure TEST_PROC IS CURSOR c1 is select sum(v.value_tx) as sum_of_values , max(c.create_dt) as max_ca
create or replace Procedure TEST_PROC IS
CURSOR c1 is
select sum(v.value_tx) as sum_of_values
, max(c.create_dt) as max_calculation_dt
, max(TRUNC(to_date((v.hr + ((v.utc_offset - 1)/24)), 'DD-MM-YY hh24:mi:ss'), 'iw')) as REPORT_PERIOD_DT
, to_char((v.hr + ((v.utc_offset - 1)/24)), 'DD-MM-YY hh24:mi:ss') as CONVERTED_HR
, v.utc_offset
, ff.form_field_label_tx
, v.region_id
, v.hr_num
, v.data_code
from value v
join calculation_value cv on v.value_id = cv.value_id
join calculation c on cv.calculation_id = c.calculation_id
join form_field ff on cv.form_field_id = ff.form_field_id
where v.hr is not null
and v.data_code is not null
and v.utc_offset is not null
group by grouping sets(
(v.region_id, v.data_code, v.hr_num, v.utc_offset, ff.form_field_label_tx, ff.form_field_id, to_char(TRUNC(to_date((v.hr + ((v.utc_offset - 1)/24)), 'DD-MM-YY hh24:mi:ss'), 'iw'), 'dddyyyy'), TRUNC(to_date((v.hr + ((v.utc_offset - 1)/24)), 'DD-MM-YY hh24:mi:ss'), 'iw'), to_char((hr + ((utc_offset - 1)/24)), 'DD-MM-YY hh24:mi:ss'), ),
(v.region_id, v.data_code, v.hr_num, v.utc_offset, ff.form_field_label_tx, ff.form_field_id, to_char(TRUNC(to_date((v.hr + ((v.utc_offset - 1)/24)), 'DD-MM-YY hh24:mi:ss'), 'iw'), 'dddyyyy'), TRUNC(to_date((v.hr + ((v.utc_offset - 1)/24)), 'DD-MM-YY hh24:mi:ss'), 'iw'), v.utc_offset), v.data_code
)
order by max(TRUNC(to_date((v.hr + ((v.utc_offset - 1)/24)), 'DD-MM-YY hh24:mi:ss'), 'iw')) desc;
l_var c1%ROWTYPE;
v_value_id value.value_id%type;
v_calculation_id calculation.calculation_id%type;
--
BEGIN
Open c1;
FETCH c1 into l_var;
insert into calculation (calculation_id, calculation_date, calculation_name, report_period_dt)
VALUES (null, sysdate, 'AGGWKL ' || l_var.REPORT_PERIOD_DT, l_var.report_period_dt)
returning calculation_id into v_calculation_id;
Close c1;
Open c1;
LOOP
FETCH c1 into l_var;
EXIT when c1%NOTFOUND;
IF l_var.utc_offset is not null
THEN
insert into value (value_id, energy_product_id, data_source_id, unit_cd, value_tx, utc_offset, data_date, hr_utc, hr, hr_num, data_code)
VALUES (null, '777', '5', 'NA', l_var.sum_of_values, l_var.utc_offset, l_var.data_date, null, null, l_var.hr_num, l_var.data_code)
returning value_id into v_value_id;
insert into calculation_value(calculation_value_id, calculation_id, value_id, form_field_id
values (null, v_calculation_id, v_value_id, l_var.form_field_id);
END LOOP;
CLOSE c1;
END TEST_PROC;
你们并没有指出在哪个表中得到空值,所以我将尝试回答我看到的唯一问题。在没有测试数据的情况下,要写出准确的答案有点困难,但据我所知,您有两个问题:
insert into calculation (calculation_id, calculation_date, calculation_name, report_period_dt)
VALUES (null, sysdate, 'AGGWKL ' || l_var.REPORT_PERIOD_DT, _var.report_period_dt) returning calculation_id into v_calculation_id;
在这里,您将null
插入calculation
表中,然后将其返回到v\u calculation\u id
类似的事情也发生在这里:
insert into value (value_id, energy_product_id, data_source_id, unit_cd,
value_tx, utc_offset, data_date, hr_utc, hr, hr_num, data_code)
VALUES (null, '777', '5', 'NA', l_var.sum_of_values, l_var.utc_offset,
l_var.data_date, null, null, l_var.hr_num, l_var.data_code) returning value_id into v_value_id;
您正在将null
插入value
表中,然后将其返回到v\u value\u id
最后,您将所有这些nulls
插入calculation\u value
中,如下所示:
insert into calculation_value(calculation_value_id, calculation_id, value_id, form_field_id
values (null, v_calculation_id (this is null), v_value_id (this is null), l_var.form_field_id);
简化你的问题@jarlh我把它简化了一点,但我认为这与把事情搞砸的分组有关,所以我认为将所有分组放在那里有助于复制/显示问题。您建议如何简化它?什么不起作用,您会遇到编译错误?一切都在编译/运行/插入中。我刚刚收到了很多被插入的条目。我猜这是因为分组的缘故,但是我得到了一些条目,比如(null,null,null,null)被插入到我的表中。我只是想知道如何“过滤”这些out@John又在家工作了?我希望如此。首先,为什么需要PL/SQL来插入?第二,是NULL,不是NULL,这是Oracle SQL的基础知识吗?选择从…起如果您的值不为空,等等…谢谢!为了澄清,我插入了一个null,因为这些ID是由触发器(序列)自动生成的