Oracle PL SQL 过程需要修改以接受具有所有类型字符的数组并拆分
Oracle PL SQL Procedure requires modification to accept arrays with all types of characters and split
我有一个程序 PUSH_DATA 以以下格式获取输入数据:
["value 1","value 2","value 3","value n"]
(所有详情见db fiddle link最后)
然后将逗号分隔值拆分为目标的数据库列 table。
现在,问题是输入字符串由最终用户传递,他有时会在两者之间引入多个双引号,主要拆分标准基于双引号。
该过程在 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 传递,而是使用数组或适合该场景的任何其他解决方案。
Fiddle 所有细节:
(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')
)
从第一个 select 查询中删除了 regex_replace
,而不是直接使用数据。
regexp_substr(data,'(".*?")(,|\])', 1, 1, 'n', 1)
null
被替换为 'n'
以便在我们在正则表达式中使用点时匹配换行符。此正则表达式执行惰性匹配,直到找到 , or ]
个字符。 ]
用于最后一个字符串。
regexp_count(data, '(".*?")(,|\])',1,'n')
更改了正则表达式,并将 null
替换为 'n'
以进行换行符匹配。
https://dbfiddle.uk/?rdbms=oracle_18&fiddle=5826bffbab9937d497245487faa04c60
我有一个程序 PUSH_DATA 以以下格式获取输入数据:
["value 1","value 2","value 3","value n"]
(所有详情见db fiddle link最后)
然后将逗号分隔值拆分为目标的数据库列 table。
现在,问题是输入字符串由最终用户传递,他有时会在两者之间引入多个双引号,主要拆分标准基于双引号。
该过程在 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 传递,而是使用数组或适合该场景的任何其他解决方案。
Fiddle 所有细节:
(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')
)
从第一个 select 查询中删除了
regex_replace
,而不是直接使用数据。regexp_substr(data,'(".*?")(,|\])', 1, 1, 'n', 1)
null
被替换为'n'
以便在我们在正则表达式中使用点时匹配换行符。此正则表达式执行惰性匹配,直到找到, or ]
个字符。]
用于最后一个字符串。regexp_count(data, '(".*?")(,|\])',1,'n')
更改了正则表达式,并将null
替换为'n'
以进行换行符匹配。
https://dbfiddle.uk/?rdbms=oracle_18&fiddle=5826bffbab9937d497245487faa04c60