无法将逗号分隔的数字字符串保存在varchar2()

时间:2019-04-01 10:06:11

标签: sql oracle

我已经有了一个想要单击的项目列表,为此,我创建了一个表,该表的列类型为varchar2(4000),在此列中我希望列出引用该ID的ID。其他表格,因此我可以将此列的值粘贴为参数。例如从table_name t中选择t。*,其中t.point_id位于(以varchar2逗号分隔的point_ids字符串)。

我遇到的问题是,当我在varchar2字段中输入多个ID时,我得到ORA-06502:PL / SQL:数字或值错误:字符到数字的转换错误

如何避免此错误?我的字段是varchar2,而不是数字,我不希望将其转换。我需要保存要解析的值。例如(11,12)

我的桌子的照片: enter image description here

编辑:注意-我的选择工作正常,我遇到的问题是保存信息。

我的插入内容:

procedure lab_water_pointsgroup (v_group_id lab_water_pointsgroups.group_name%type,
                          v_group_name lab_water_pointsgroups.group_code%type,
                          v_group_code lab_water_pointsgroups.lab_points_ids%type,
                          v_lab_points_ids lab_water_pointsgroups.group_id%type) as
begin
update lab_water_pointsgroups
   set group_name = v_group_name,
       group_code = v_group_code,
       lab_points_ids = v_lab_points_ids
 where  group_id = v_group_id;
if ( SQL%RowCount = 0 ) then
insert into lab_water_pointsgroups
  (group_id, group_name, group_code, lab_points_ids)
values
  (v_group_id, v_group_name, v_group_code, v_lab_points_ids);
end if;
end;

3 个答案:

答案 0 :(得分:2)

由于您没有提供任何示例,因此不确定在此方面我能为您提供多少帮助。看看下面的演示,也许用xmltable的构造可以解决您的问题。 HTH KR

create table testtab (id number);
insert into  testtab values (1);

select * from testtab where id in ('1');   -- works
select * from testtab where id in (1);     -- works
select * from testtab where id in (1,2);   -- works
select * from testtab where id in ('1,2'); -- ORA-01722: invalid number
select * from testtab where id in (select to_number(xt.column_value) from xmltable('1,2') xt); -- works

答案 1 :(得分:1)

以下是您为过程定义参数的方式:

v_group_id        lab_water_pointsgroups.group_name%type,
v_group_name      lab_water_pointsgroups.group_code%type,
v_group_code      lab_water_pointsgroups.lab_points_ids%type,
v_lab_points_ids  lab_water_pointsgroups.group_id%type

我怀疑您在类型方面犯了错误,因为id具有name类型,name具有code类型,等等。所以应该是:

v_group_id        lab_water_pointsgroups.group_id%type,
v_group_name      lab_water_pointsgroups.group_name%type, 
v_group_code      lab_water_pointsgroups.group_code%type,
v_lab_points_ids  lab_water_pointsgroups.lab_points_ids%type

我建议使用merge代替此update / insert,但这不是您要的:)

答案 2 :(得分:0)

您的错误是因为您没有区别包含逗号分隔数字的变量和“ in”运算符中的实际枚举。在代码分析并准备执行之后,您的语句将像.. id in ('1,2,3')而不是..id in (1,2,3)一样,您是否注意到差异?因此,您需要将逗号分隔的值转换为数组,或者在这种情况下转换为集合。您的代码应如下所示:

select t.*
  from table_name t
 where t.point_id in
       (select regexp_substr(YOUR_VARCHAR2_COLUMN_VALUE, '[^,]+', 1, level)
          from dual
        connect by regexp_substr(YOUR_VARCHAR2_COLUMN_VALUE, '[^,]+', 1, level) is not null)
相关问题