我有一个 PUSH_DATA 过程,它接受以下格式的输入数据:
["value 1","value 2","value 3","value n"]
(所有细节都可以在最后的 db fiddle 链接中找到)
然后它将逗号分隔值拆分为目标表的数据库列。
现在,问题是输入字符串由最终用户传递,他有时会在两者之间引入多个双引号,主要拆分标准基于双引号。
该过程以 fiddle 形式出现在此处,最后传递了一个字符串示例来演示该问题。
所以当过程被传递为:
BEGIN
PUSH_DATA(110,'["Project title afor BYU heads","The values are,\n "exactly" up to the requirement and analysis done by the team.
Also it is difficult to,\n prepare a scenario notwithstanding the fact it is difficult. This user story is going to be slightly complex however it is up to the team","Active","Disabled","25 tonnes of fuel","www.examplesites.com/html.asp&net;","Apprehension","","","","25","Stable"]');
END;
/
结果符合预期:
当字符串传递时带有额外的引号如下:
BEGIN
PUSH_DATA(110,'[""Project title afor BYU heads"","The values are,\n "exactly" up to the requirement and analysis done by the team.
Also it is difficult to,\n prepare a scenario notwithstanding the fact it is difficult. This user story is going to be slightly complex however it is up to the team","Active","Disabled","25 tonnes of fuel","www.examplesites.com/html.asp&net;","Apprehension","","","","25","Stable"]');
END;
/
由于额外的引号,结果出乎意料:
要求是忽略引号内的任何特殊字符,因为它应该显示而不是被评估而忽略,例如额外的双引号或/或\将被忽略并按原样传递字符串。强>
所以如果传递一个值,"Hi There""s, No"
输出应为 Hi there""s, No
.
因此,我需要修改解决方案,这样
- 使用现有的正则表达式条件并添加条件以在双引号内包含双引号
- 或者,不是将参数作为 varchar 传递,而是使用数组或适合该场景的任何其他解决方案。
摆弄所有细节:
最佳答案
(select data_string from dual),
rcte (id, data, lvl, result)
AS (
SELECT p_id, data, 1,regexp_substr(data,
'(".*?")(,|\])', 1, 1, 'n', 1) result
FROM (select data_string data from input)
UNION ALL
SELECT id, data, lvl + 1,
regexp_substr(data, '(".*?")(,|\])', 1, lvl + 1, 'n', 1)
FROM rcte
WHERE lvl <= regexp_count(data, '(".*?")(,|\])',1,'n')
)
从第一个选择查询中删除了
regex_replace
,而不是直接使用数据。regexp_substr(data,'(".*?")(,|\])', 1, 1, 'n', 1)
null
被替换为'n'
以便在我们在正则表达式中使用点时匹配换行符。此正则表达式执行惰性匹配,直到找到或 ]
字符。]
用于最后一个字符串。regexp_count(data, '(".*?")(,|\])',1,'n')
更改了正则表达式并替换了null
与'n'
匹配换行符。
https://dbfiddle.uk/?rdbms=oracle_18&fiddle=5826bffbab9937d497245487faa04c60
关于sql - Oracle PL SQL Procedure 需要修改以接受具有所有类型字符的数组并拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63717474/