0

我有一个名为 product 的表,其中包含一个名为“Description”的列。描述列的值将类似于

'NAME:ITEM1;COST:20;QUANTITY:23;'
'NAME:ITEM2;COST:20;QUANTITY:23;'
'NAME:ITEM4;COST:24;QUANTITY:24;'
'NAME:ITEM6;COST:26;QUANTITY:25;'
'NAME:ITEM3;COST:27;QUANTITY:27;'

现在我有另一个名为的表,PRODUCT_DETAILS它有三列NAME, COST, QUANTITY

我必须拆分值':',';'并将值单独提取到PRODUCT_DETAILS表中。

我应该使用存储过程来做到这一点。请帮我解决这个问题,因为我只用 SQL 编写了简单的查询和存储过程

4

2 回答 2

1

您不需要为此使用存储过程。正如您知道描述的格式,您可以轻松地选择值并将它们插入到 product_details 中:

插入 product_details
(名称、成本、数量)
选择
  substr(description, instr(description, ':', 1, 1) + 1, instr(description, ';', 1, 1) - instr(description, ':', 1, 1) - 1) 作为名称,
  to_number(substr(description, instr(description, ':', 1, 2) + 1, instr(description, ';', 1, 2) - instr(description, ':', 1, 2) - 1))作为成本,
  to_number(substr(description, instr(description, ':', 1, 3) + 1, instr(description, ';', 1, 3) - instr(description, ':', 1, 3) - 1))作为数量
来自产品;

当然你也可以写一个包含语句的过程:

创建或替换过程 product_to_product_details 是
开始
  插入 product_details
  (名称、成本、数量)
  选择
    substr(description, instr(description, ':', 1, 1) + 1, instr(description, ';', 1, 1) - instr(description, ':', 1, 1) - 1) 作为名称,
    to_number(substr(description, instr(description, ':', 1, 2) + 1, instr(description, ';', 1, 2) - instr(description, ':', 1, 2) - 1))作为成本,
    to_number(substr(description, instr(description, ':', 1, 3) + 1, instr(description, ';', 1, 3) - instr(description, ':', 1, 3) - 1))作为数量
  来自产品;
结尾;

于 2013-09-19T18:49:13.553 回答
0

这是一个示例查询,可帮助您拆分数据:

SELECT REGEXP_REPLACE(str,'.*NAME:([^;]+);.*','\1') AS name
  ,REGEXP_REPLACE(str,'.*COST:([^;]+);.*','\1') AS cost
  ,REGEXP_REPLACE(str,'.*QUANTITY:([^;]+);.*','\1') AS quantity
FROM SplitStringTest;

这是一个Fiddle来演示。正则表达式是处理这类事情的一个非常方便的工具。

以下是一些参考资料:

正则表达式教程

甲骨文文档

于 2013-09-19T19:34:28.017 回答