【问题标题】:Parse CSV data for SQL Insert Query Using Regex使用正则表达式为 SQL 插入查询解析 CSV 数据
【发布时间】:2021-12-28 16:54:36
【问题描述】:

我需要一些正则表达式的帮助,拜托。我正在尝试使用 Integromat 获取一些数据,然后将其解析为适合 SQL INSERT 命令的格式。 Integromat 使用 Javascript 正则表达式。

初始数据如下所示:

Identifier,Handle,Type
CS18511,big-3-jersey,men's jerseys
CS185110231,big-3-jersey,men's jerseys
CS185110232,big-3-jersey,men's jerseys
CS185110233,big-3-jersey,men's jerseys

我想应用正则表达式替换来实现这种格式,最好没有标题行。

('CS18511','big-3-jersey,men''s' jerseys'),
('CS185110231','big-3-jersey,men''s jerseys'),
('CS185110232','big-3-jersey,men''s jerseys'),
('CS185110233','big-3-jersey,men''s jerseys')

我已尝试以下方法,它似乎对我想要的项目进行了分组,但找不到可用于创建所需输出的替代项。

((?=[^\s,])(?: ?[^[\s,]+|\[[^]]*])*|(?<=,|^)(?=\s*(?:,|$)))

非常感谢任何帮助!

干杯

【问题讨论】:

  • 为什么????可以直接用BULK INSERT插入数据

标签: sql sql-server regex integromat


【解决方案1】:

这是一个关于如何使用BULK INSERT的工作示例

Create Table #YourTable (Identifier varchar(50),Handle varchar(50),Type varchar(50))
Bulk Insert  #YourTable FROM 'C:\Working\MyData.csv' WITH ( FIRSTROW = 2,
                                                            FIELDTERMINATOR = ',', 
                                                            ROWTERMINATOR = '\n' 
                                                          )
Select * from #YourTable

结果

Identifier   Handle         Type
CS18511      big-3-jersey   men's jerseys
CS185110231  big-3-jersey   men's jerseys
CS185110232  big-3-jersey   men's jerseys
CS185110233  big-3-jersey   men's jerseys

【讨论】:

  • 非常感谢约翰...我不知道批量插入。不幸的是,CSV 数据是通过对我们 PIM 的 API 调用动态创建的,因此它不是一个有位置可供参考的物理文件。
  • 这是失败的输出:CREATE TABLE PIM_DATA ( Identifier VARCHAR(100), Handle VARCHAR(100), Type VARCHAR(100) ) END BEGIN BULK INSERT PIM_DATA FROM '"Identifier,Handle,Shopify Type CS18511,big-3-jersey,men's jerseys CS185110231,big-3-jersey,men's jerseys CS185110232,big-3-jersey,men's jerseys etc...
  • @tbizzlebozzle 批量插入假定为源文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-18
  • 2011-08-10
  • 2012-07-01
  • 1970-01-01
  • 2012-05-18
相关资源
最近更新 更多