如何将数据从.txt文件导入到SQL中

huangapple go评论101阅读模式
英文:

How to pull data from a .txt file to sql

问题

需要从.txt文件中提取数据并插入到SQL Server表中。这个文件每天从1次到X次不等地更新,每次会添加3到5行新数据,具体取决于需要,并且每天的txt文件名称都会更改,例如今天的txt文件名是:2023-03-03。我计划每小时使用一个作业来提取这些数据,但我的问题是如何将下面的数据插入到SQL Server表中?

22:54:18 [JewelOfHarmonyItemRestoreMix][Success][User7][Say380] - (ChaosSuccessRate: 0, ChaosMoney: 515000)
23:16:25 [SocketItemCreateSeedMix][Success][User7][Jack380] - (ChaosSuccessRate: 100, ChaosMoney: 1030000)
13:03:31 [PlusItemLevelMix][Success][User22][Blanca] - (Type: 0, ChaosSuccessRate: 60, ChaosMoney: 2060000)

我想要将信息以以下的方式插入到我的表中:

时间 类型 状态 账户ID 用户 比率 混沌货币
22:54:18 JewelOfHarmonyItemRestoreMix Success User7 Say380 0 515000
23:16:25 SocketItemCreateSeedMix Success User7 Jack380 100 1030000

这是要提取数据的TXT文档:

22:54:18 ============================= START MIX ============================= 
22:54:18 [Slot 01](Name: Dragon Knight, Index: 5149, Level: 15, Dur: 152, Serial: 0000D0A7, Option1: 0, Option2: 1, Option3: 7, NewOpt: 014, SetOpt: 000, JOH: 141, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
22:54:18 [JewelOfHarmonyItemRestoreMix][Success][User7][Say380] - (ChaosSuccessRate: 0, ChaosMoney: 515000)
23:16:25 ============================= START MIX ============================= 
23:16:25 [Slot 01](Name: Sacred, Index: 4667, Level: 04, Dur: 76, Serial: 000177D6, Option1: 0, Option2: 0, Option3: 1, NewOpt: 000, SetOpt: 005, JOH: 000, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
23:16:25 [Slot 03](Name: Daybreak, Index: 0024, Level: 04, Dur: 109, Serial: 000177D2, Option1: 0, Option2: 0, Option3: 1, NewOpt: 002, SetOpt: 000, JOH: 000, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
23:16:25 [Slot 05](Name: Jewel of Chaos, Index: 6159, Level: 00, Dur: 1, Serial: 000177D3, Option1: 0, Option2: 0, Option3: 0, NewOpt: 000, SetOpt: 000, JOH: 000, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
23:16:25 [SocketItemCreateSeedMix][Success][User7][Jack380] - (ChaosSuccessRate: 100, ChaosMoney: 1030000)
13:03:31 ============================= START MIX ============================= 
13:03:31 [Slot 01](Name: Jewel of Soul, Index: 7182, Level: 00, Dur: 1, Serial: 00017912, Option1: 0, Option2: 0, Option3: 0, NewOpt: 000, SetOpt: 000, JOH: 000, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
13:03:31 [Slot 02](Name: Jewel of Bless, Index: 7181, Level: 00, Dur: 1, Serial: 0001457A, Option1: 0, Option2: 0, Option3: 0, NewOpt: 000, SetOpt: 000, JOH: 000, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
13:03:31 [Slot 03](Name: Jewel of Chaos, Index: 6159, Level: 00, Dur: 1, Serial: 00014253, Option1: 0, Option2: 0, Option3: 0, NewOpt: 000, SetOpt: 000, JOH: 000, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
13:03:31 [PlusItemLevelMix][Success][User22][Blanca] - (Type: 0, ChaosSuccessRate: 60, ChaosMoney: 2060000)

希望这些信息能帮助你将数据插入到SQL Server表中。

英文:

I need to pull data from a .txt file to my SQL Server into a table.

This file is constantly updating from 1 to X amount of times per day giving from 3 to 5 new rows depending on needs and txt file name changes every day example today txt name is: 2023-03-03. I'm going to use a Job every hour that pulls this data but my issue is.

How can I get the data below into my table in sql server?

22:54:18 [JewelOfHarmonyItemRestoreMix][Success][User7][Say380] - (ChaosSuccessRate: 0, ChaosMoney: 515000)
23:16:25 [SocketItemCreateSeedMix][Success][User7][Jack380] - (ChaosSuccessRate: 100, ChaosMoney: 1030000)
13:03:31 [PlusItemLevelMix][Success][User22][Blanca] - (Type: 0, ChaosSuccessRate: 60, ChaosMoney: 2060000)

I want that info like this in my table

Time Type State AccountID User Rate ChaosMoney
22:54:18 JewelOfHarmonyItemRestoreMix Success User7 Say380 0 515000
23:16:25 SocketItemCreateSeedMix Success User7 Jack380 100 1030000

This is the TXT document to pull data from

22:54:18 ============================= START MIX ============================= 
22:54:18 [Slot 01](Name: Dragon Knight, Index: 5149, Level: 15, Dur: 152, Serial: 0000D0A7, Option1: 0, Option2: 1, Option3: 7, NewOpt: 014, SetOpt: 000, JOH: 141, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
22:54:18 [JewelOfHarmonyItemRestoreMix][Success][User7][Say380] - (ChaosSuccessRate: 0, ChaosMoney: 515000)
23:16:25 ============================= START MIX ============================= 
23:16:25 [Slot 01](Name: Sacred, Index: 4667, Level: 04, Dur: 76, Serial: 000177D6, Option1: 0, Option2: 0, Option3: 1, NewOpt: 000, SetOpt: 005, JOH: 000, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
23:16:25 [Slot 03](Name: Daybreak, Index: 0024, Level: 04, Dur: 109, Serial: 000177D2, Option1: 0, Option2: 0, Option3: 1, NewOpt: 002, SetOpt: 000, JOH: 000, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
23:16:25 [Slot 05](Name: Jewel of Chaos, Index: 6159, Level: 00, Dur: 1, Serial: 000177D3, Option1: 0, Option2: 0, Option3: 0, NewOpt: 000, SetOpt: 000, JOH: 000, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
23:16:25 [SocketItemCreateSeedMix][Success][User7][Jack380] - (ChaosSuccessRate: 100, ChaosMoney: 1030000)
13:03:31 ============================= START MIX ============================= 
13:03:31 [Slot 01](Name: Jewel of Soul, Index: 7182, Level: 00, Dur: 1, Serial: 00017912, Option1: 0, Option2: 0, Option3: 0, NewOpt: 000, SetOpt: 000, JOH: 000, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
13:03:31 [Slot 02](Name: Jewel of Bless, Index: 7181, Level: 00, Dur: 1, Serial: 0001457A, Option1: 0, Option2: 0, Option3: 0, NewOpt: 000, SetOpt: 000, JOH: 000, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
13:03:31 [Slot 03](Name: Jewel of Chaos, Index: 6159, Level: 00, Dur: 1, Serial: 00014253, Option1: 0, Option2: 0, Option3: 0, NewOpt: 000, SetOpt: 000, JOH: 000, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
13:03:31 [PlusItemLevelMix][Success][User22][Blanca] - (Type: 0, ChaosSuccessRate: 60, ChaosMoney: 2060000)

答案1

得分: 1

请尝试以下解决方案。

它将从SQL Server 2017开始运行,因为它依赖于TRIM()函数。

它使用SQL Server的XML和XQuery来标记化每一行。

我将您的示例数据保存在**'e:\Temp\NachoSanchez.txt'**文件中。

并创建了一个格式文件**'e:\Temp\NachoSanchez-format.xml'**如下所示:

<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <RECORD>
      <FIELD ID="1" xsi:type="CharTerm" TERMINATOR="\r\n" MAX_LENGTH="2048" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
   </RECORD>
   <ROW>
      <COLUMN SOURCE="1" NAME="line" xsi:type="SQLVARYCHAR"/>
   </ROW>
</BCPFORMAT>

SQL

DECLARE @separator CHAR(1) = SPACE(1);

;WITH rs (line) AS
(
   SELECT line
   FROM  OPENROWSET(BULK 'e:\Temp\NachoSanchez.txt'
      , FORMATFILE = 'e:\Temp\NachoSanchez-format.xml'  
      , ERRORFILE = 'e:\Temp\NachoSanchez-log.err'
      , FIRSTROW = 1 -- real data starts on the 1st row
      , MAXERRORS = 100
   ) AS tbl
)
SELECT x.value('(/root/r[1]/text())[1]', 'CHAR(8)') AS [time]
	, TRIM('[]' FROM x.value('(/root/r[2]/text())[1]', 'VARCHAR(50)')) AS [Type]
	, TRIM('[]' FROM x.value('(/root/r[3]/text())[1]', 'VARCHAR(50)')) AS [State]
	, TRIM('[]' FROM x.value('(/root/r[4]/text())[1]', 'VARCHAR(50)')) AS AccountID
	, TRIM('[]' FROM x.value('(/root/r[5]/text())[1]', 'VARCHAR(50)')) AS [User]
	, TRIM(',' FROM x.value('(/root/r[last() - 2]/text())[1]', 'VARCHAR(50)')) AS [Rate]
	, TRIM(')' FROM x.value('(/root/r[last()]/text())[1]', 'VARCHAR(50)')) AS [ChaosMoney]
FROM rs
CROSS APPLY (SELECT TRY_CAST('<root><r><![CDATA[' + 
    REPLACE(REPLACE(line,'][','] ['), @separator,']]></r><r><![CDATA[') + 
    ']]></r></root>' AS XML)) AS t(x)
WHERE line LIKE '%User%';

Output

time Type State AccountID User Rate ChaosMoney
13:03:31 PlusItemLevelMix Success User22 Blanca 60 2060000
22:54:18 JewelOfHarmonyItemRestoreMix Success User7 Say380 0 515000
23:16:25 SocketItemCreateSeedMix Success User7 Jack380 100 1030000
英文:

Please try the following solution.

It will work starting from SQL Server 2017 onwards due to dependency on the TRIM() function.

It is using SQL Server's XML and XQuery to tokenize each line.

I saved your sample data in the 'e:\Temp\NachoSanchez.txt' file.

And created a format file 'e:\Temp\NachoSanchez-format.xml' as follows:

&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;BCPFORMAT xmlns=&quot;http://schemas.microsoft.com/sqlserver/2004/bulkload/format&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt;
   &lt;RECORD&gt;
      &lt;FIELD ID=&quot;1&quot; xsi:type=&quot;CharTerm&quot; TERMINATOR=&#39;\r\n&#39; MAX_LENGTH=&quot;2048&quot; COLLATION=&quot;SQL_Latin1_General_CP1_CI_AS&quot;/&gt;
   &lt;/RECORD&gt;
   &lt;ROW&gt;
      &lt;COLUMN SOURCE=&quot;1&quot; NAME=&quot;line&quot; xsi:type=&quot;SQLVARYCHAR&quot;/&gt;
   &lt;/ROW&gt;
&lt;/BCPFORMAT&gt;

SQL

DECLARE @separator CHAR(1) = SPACE(1);

;WITH rs (line) AS
(
   SELECT line
   FROM  OPENROWSET(BULK &#39;e:\Temp\NachoSanchez.txt&#39;
      , FORMATFILE = &#39;e:\Temp\NachoSanchez-format.xml&#39;  
      , ERRORFILE = &#39;e:\Temp\NachoSanchez-log.err&#39;
      , FIRSTROW = 1 -- real data starts on the 1st row
      , MAXERRORS = 100
   ) AS tbl
)
SELECT x.value(&#39;(/root/r[1]/text())[1]&#39;, &#39;CHAR(8)&#39;) AS [time]
	, TRIM(&#39;[]&#39; FROM x.value(&#39;(/root/r[2]/text())[1]&#39;, &#39;VARCHAR(50)&#39;)) AS [Type]
	, TRIM(&#39;[]&#39; FROM x.value(&#39;(/root/r[3]/text())[1]&#39;, &#39;VARCHAR(50)&#39;)) AS [State]
	, TRIM(&#39;[]&#39; FROM x.value(&#39;(/root/r[4]/text())[1]&#39;, &#39;VARCHAR(50)&#39;)) AS AccountID
	, TRIM(&#39;[]&#39; FROM x.value(&#39;(/root/r[5]/text())[1]&#39;, &#39;VARCHAR(50)&#39;)) AS [User]
	, TRIM(&#39;,&#39; FROM x.value(&#39;(/root/r[last() - 2]/text())[1]&#39;, &#39;VARCHAR(50)&#39;)) AS [Rate]
	, TRIM(&#39;)&#39; FROM x.value(&#39;(/root/r[last()]/text())[1]&#39;, &#39;VARCHAR(50)&#39;)) AS [ChaosMoney]
FROM rs
CROSS APPLY (SELECT TRY_CAST(&#39;&lt;root&gt;&lt;r&gt;&lt;![CDATA[&#39; + 
    REPLACE(REPLACE(line,&#39;][&#39;,&#39;] [&#39;), @separator, &#39;]]&gt;&lt;/r&gt;&lt;r&gt;&lt;![CDATA[&#39;) + 
    &#39;]]&gt;&lt;/r&gt;&lt;/root&gt;&#39; AS XML)) AS t(x)
WHERE line LIKE &#39;%User%&#39;;

Output

time Type State AccountID User Rate ChaosMoney
13:03:31 PlusItemLevelMix Success User22 Blanca 60 2060000
22:54:18 JewelOfHarmonyItemRestoreMix Success User7 Say380 0 515000
23:16:25 SocketItemCreateSeedMix Success User7 Jack380 100 1030000

答案2

得分: 0

以下是已翻译的内容:

基本上,我们使用 BULK SINGLE_CLOB 加载整个文件,然后按换行符分割它。

然后,我们剪切消息的各个部分,然后将它们组装成一个更简单的可用于工作的 JSON 数组版本。最后,我们为每个数组值创建列以获取结果。

英文:

Here's another version which uses more old hat string manipulation techniques:

declare @file nvarchar(max)

-- Load from file
  
SELECT @file = BulkColumn
FROM OPENROWSET(BULK &#39;c:\path-to-your-file.txt&#39;, SINGLE_CLOB) AS DATA;

-- For testing only
set @file = N&#39;22:54:18 ============================= START MIX ============================= 
22:54:18 [Slot 01](Name: Dragon Knight, Index: 5149, Level: 15, Dur: 152, Serial: 0000D0A7, Option1: 0, Option2: 1, Option3: 7, NewOpt: 014, SetOpt: 000, JOH: 141, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
22:54:18 [JewelOfHarmonyItemRestoreMix][Success][User7][Say380] - (ChaosSuccessRate: 0, ChaosMoney: 515000)
23:16:25 ============================= START MIX ============================= 
23:16:25 [Slot 01](Name: Sacred, Index: 4667, Level: 04, Dur: 76, Serial: 000177D6, Option1: 0, Option2: 0, Option3: 1, NewOpt: 000, SetOpt: 005, JOH: 000, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
23:16:25 [Slot 03](Name: Daybreak, Index: 0024, Level: 04, Dur: 109, Serial: 000177D2, Option1: 0, Option2: 0, Option3: 1, NewOpt: 002, SetOpt: 000, JOH: 000, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
23:16:25 [Slot 05](Name: Jewel of Chaos, Index: 6159, Level: 00, Dur: 1, Serial: 000177D3, Option1: 0, Option2: 0, Option3: 0, NewOpt: 000, SetOpt: 000, JOH: 000, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
23:16:25 [SocketItemCreateSeedMix][Success][User7][Jack380] - (ChaosSuccessRate: 100, ChaosMoney: 1030000)
13:03:31 ============================= START MIX ============================= 
13:03:31 [Slot 01](Name: Jewel of Soul, Index: 7182, Level: 00, Dur: 1, Serial: 00017912, Option1: 0, Option2: 0, Option3: 0, NewOpt: 000, SetOpt: 000, JOH: 000, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
13:03:31 [Slot 02](Name: Jewel of Bless, Index: 7181, Level: 00, Dur: 1, Serial: 0001457A, Option1: 0, Option2: 0, Option3: 0, NewOpt: 000, SetOpt: 000, JOH: 000, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
13:03:31 [Slot 03](Name: Jewel of Chaos, Index: 6159, Level: 00, Dur: 1, Serial: 00014253, Option1: 0, Option2: 0, Option3: 0, NewOpt: 000, SetOpt: 000, JOH: 000, ItemOptEx: 000, SocketOpt: 255, 255, 255, 255, 255)
13:03:31 [PlusItemLevelMix][Success][User22][Blanca] - (Type: 0, ChaosSuccessRate: 60, ChaosMoney: 2060000)
&#39;
    
select SUBSTRING(x.Value, 1, dateSep -1) AS dt
, ROW_NUMBER() OVER(ORDER BY @@SPID) AS id
, z.*
FROM STRING_SPLIT(@file, CHAR(10)) x
CROSS APPLY (
      select charindex(&#39; &#39;, x.value) AS dateSep
      , charindex(&#39; - &#39;, x.value) AS chaosSep
      , charindex(&#39;(&#39;, x.value) AS chaosStart
      , charindex(&#39;)&#39;, x.value) AS chaosEnd
  ) y
CROSS APPLY (
     SELECT MAX(case when [key] = 0 then value end) as item
     , MAX(case when [key] = 1 then value end) as status
     , MAX(case when [key] = 2 then value end) as userid
     , MAX(case when [key] = 3 then value end) as userName
     , MAX(case when [key] = 4 then JSON_VALUE(value, &#39;$.ChaosSuccessRate&#39;) end) as chaosRate
     , MAX(case when [key] = 4 then JSON_VALUE(value, &#39;$.ChaosMoney&#39;) end) as chaosMoney
     FROM OPENJSON(&#39;[&quot;&#39; + replace(replace(substring(x.value, dateSep + 2, chaosSep - dateSep-3), &#39;]&#39;,&#39;&quot;,&#39;), &#39;[&#39;, &#39;&quot;&#39;) + &#39;&quot;&#39;
   + &#39;,{&quot;&#39; + replace(replace(substring(x.value, chaosStart + 1, chaosEnd - chaosStart - 1), &#39;:&#39;, &#39;&quot;:&#39;), &#39;, &#39;, &#39;, &quot;&#39;) + &#39;}]&#39;) AS details
  ) z
WHERE x.value LIKE &#39;%ChaosMoney%&#39;

Basicly, we load whole file using BULK SINGLE_CLOB and split it by newlines.

Then we cut out various parts of the message and then assemble them together in a more simpler to work with json array version. Finally, we create column for each array value to get the results.

huangapple
  • 本文由 发表于 2023年3月4日 00:56:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定