SQL Server更新查询中的正则表达式替换

时间:2018-11-30 12:13:35

标签: sql sql-server regex

我已在表中添加了新列,并希望对其进行预填充。

此刻我有这个:

update Table_A
set packages = 'a:1:{i:0;a:2:{s:4:"name"; s:' + ltrim(str(len(p.name))) + ':"' + p.name + '";'+
                's:11:"category_id"; a:1:{i:0;s:1:"0";}}}'
from broker_product as Table_A
join scheme s on Table_A.scheme_id = s.id
join product p on p.id = s.product_ids
where Table_A.packages is null

我想做的是根据category_id中一列的值填充product,而不是现在的固定字符串。

product.risks中的示例是:

a:2:{i:0;a:5:{s:10:"field_name";s:24:"Gadget Accidental Damage";s:19:"risk_category_value";s:4:"1.00";s:13:"variable_name";s:24:"gadget-accidental-damage";s:11:"description";s:0:"";s:10:"hover_text";s:0:"";}i:1;a:5:{s:10:"field_name";s:32:"Home Emergency Accidental Damage";s:19:"risk_category_value";s:4:"2.00";s:13:"variable_name";s:0:"";s:11:"description";s:0:"";s:10:"hover_text";s:0:"";}}

在此示例中,我希望category_id为:

 a:2:{i:0;s:1:"0";i:1;s:1:"1";}

是与products.risk相同长度的数组,其大小为i:0;s:1:"0";,计算product.risks中的元素数。

例如,我正在寻找类似的东西(使用正确的正则表达式,而不是我提供的损坏的东西):

update Table_A
set packages = 'a:1:{i:0;a:2:{s:4:"name"; s:' + ltrim(str(len(p.name))) + ':"' + p.name + '";'
                's:11:"category_id"; ' + preg_replace('/a:(\d+):{(i:\d+;)/', 'a:$1:{$2s:1:"0";}', p.risks) + '}}'
from broker_product as Table_A
join scheme s on Table_A.scheme_id = s.id
join product p on p.id = s.product_ids
where Table_A.packages is null

0 个答案:

没有答案