CSV字符串中的SQL Server 2012更新特定列
我正在寻找一些帮助编码的东西。我们正在尝试更新我拥有的CSV字符串的特定“列” 下面是一个屏幕截图来说明我的观点: 列“DataRow”有一个CSV字符串,我需要修改它。使用CHARINDEX很容易找到第一列,但我似乎无法超越这一点。我试图更新字符串中的第三个值,如果是第一个记录,则为“TM-沙漠风别墅..” 有人有什么建议吗CSV字符串中的SQL Server 2012更新特定列,sql,sql-server,csv,Sql,Sql Server,Csv,我正在寻找一些帮助编码的东西。我们正在尝试更新我拥有的CSV字符串的特定“列” 下面是一个屏幕截图来说明我的观点: 列“DataRow”有一个CSV字符串,我需要修改它。使用CHARINDEX很容易找到第一列,但我似乎无法超越这一点。我试图更新字符串中的第三个值,如果是第一个记录,则为“TM-沙漠风别墅..” 有人有什么建议吗 非常感谢 我想您可以使用where子句,如下所示,我只是选择(ing)数据,但显然您可以使用Update语句来更新返回的数据 测试数据 DECLARE @T TABLE
非常感谢 我想您可以使用where子句,如下所示,我只是选择(ing)数据,但显然您可以使用Update语句来更新返回的数据 测试数据
DECLARE @T TABLE (ID INT, Data VARCHAR(100))
INSERT INTO @T
VALUES (1, '1,ABC,HZY'),(2, '2,DEF,XYZ'),(3, '3,ABC,MNP')
SELECT * FROM @T
╔════╦════════════╗
║ ID ║ Data ║
╠════╬════════════╣
║ 1 ║ 1,ABC,HZY ║
║ 2 ║ 2,DEF,XYZ ║
║ 3 ║ 3,ABC,MNP ║
╚════╩════════════╝
查询
SELECT *
FROM @T
WHERE (',' + Data + ',') LIKE '%MNP%'
╔════╦════════════╗
║ ID ║ Data ║
╠════╬════════════╣
║ 3 ║ 3,ABC,MNP ║
╚════╩════════════╝
在我给你代码之前,我想说,如果可以的话,改变这个设计强> 数据库应该有表和列。使用分隔字符串违反了标准格式 因此,我使得操作数据(D.M.L.~UPD、DEL、INS)变得更加困难 有了这一点,我用下面的算法解决了你的问题
1 - Create and load sample table with data.
2 - Choose a string split function, see article from Aaron Bertrand on choices.
3 - Split the string and store Items by Key, Column number order in a staging table 1
4 - Update the staging table 1. I did it for all column 2's.
However, there could be a where clause to do partial updates.
Also you could use update_flag to identify changed rows.
5 - Repackage the items as a string. I saved this step in staging table 2.
Can leverage update_flag to only packaged changed rows from staging table 1.
6 - Update the original table with new strings.
下面是本练习的示例代码
-- Just playing
use tempdb;
-- Drop the sample table
if OBJECT_ID('my_data') > 0
drop table my_data
go
-- Drop stage table 1
if OBJECT_ID('stage1') > 0
drop table stage1
go
-- Drop stage table 2
if OBJECT_ID('stage2') > 0
drop table stage2
go
-- Create sample table
create table my_data
(data_id INT, data_delimited_txt VARCHAR(100));
-- Add data
insert into my_data
values (1, '1,ABC,HZY'),(2, '2,DEF,XYZ'),(3, '3,ABC,MNP');
-- Show the data
select * from my_data;
go
-- http://www.sqlperformance.com/2012/07/t-sql-queries/split-strings
-- String split function, save 2 stage1
select
d.data_id,
row_number() over (partition by d.data_id order by (select 0)) as row_id,
s.item
into
stage1
from
my_data d cross apply
msdb.dbo.SplitStrings_XML(d.data_delimited_txt, ',') as s
GO
-- Update second (col/val) in string
update stage1
set item = 'NEW VALUE'
where row_id = 2
go
-- Get updated string in stage2
select
data_id,
stuff(
(
SELECT ', ' + ISNULL(inner1.item,'')
FROM #stage as inner1
WHERE inner1.data_id = outer1.data_id
FOR XML PATH('')
) , 1, 2, '') as list_txt
into stage2
from stage1 as outer1
group by data_id
go
-- Update original table
update my_data
set data_delimited_txt = list_txt
from my_data m join stage2 s on m.data_id = s.data_id
go
-- Show the data
select * from my_data;
go
我将更新标志(upd_标志)和任何步骤消除之类的增强留给您去做
此外,XML拆分函数依赖于有效的XML数据。如果这是一个问题,该代码的生产版本应该使用另一个拆分函数
祝你好运
问题1。为什么要以这种方式存储数据?为什么不将所有CSV信息存储在不同的列中?嗨,赞恩,谢谢你的回复。这是一个我们正在尝试更新的预填充表。我们无法控制数据的存储方式。有一些方法可以拆分csv字符串。我建议使用CLR过程,这样您可以拆分字符串(string.split),编辑第三个数组元素,然后将数组元素重新连接在一起(string.join)。您好@M.Ali,非常感谢您的代码。据我所知,为了以这种方式更新,我们必须知道需要更新的模式或字符串中的字符,对吗?在这种情况下,我们将遇到一个问题,因为我们不知道每次需要更新字符串的内容。我们所确定的只是“列”在CSV字符串中的位置;希望这更有意义?再一次,非常感谢你抽出时间来帮助我。我很感激,非常感谢!该代码完全按照需要工作!我同意你关于急需的设计变更的意见,但这超出了我们的范围;因为它是ERP系统使用的内部表。他们使用此表导入CSV文件,并使用内部存储过程对其进行操作。我们希望在流程进入下一步之前更新此表中的值。同样,这是您提供的非常有价值的信息,毫无疑问将为我们节省大量时间。再次感谢你。