解析存储在SQL Server表格单元格中的INI文件长文本字符串。

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

Parse a long text string of the ini file stored in a cell of the SQL Server table

问题

Here's the translation of the text you provided:

帮助我解析存储在 SQL Server 表格单元格中的 INI 文件的长文本字符串。

我有一个包含列 id, rawINI 的表格。

rawINI 列中,INI 文件的结构如下所示:

[BlockName1]
Key1=value
Key2=value1,value2,value3
Key3=
[BlockName1]
Key1=value
Key2=value1,value2,value3
Key3=

需要将这个文本转换成新表格的形式,例如(也许你可以推荐另一种方式):

Block,Key,Value
--------------------------------------
BlockName1,Key1,value
BlockName1,Key2,value1,value2,value3
BlockName1,Key3
BlockName2,Key1,value
BlockName2,Key2,value1,value2,value3
BlockName2,Key3

我不熟悉 T-SQL,也不知道从何处开始解决这个问题。如何解决这个问题?有哪些功能可以帮助?

英文:

Help me parse a long text string of the ini file stored in a cell of the SQL Server table.

I have a table with columns id, rawINI.

In the rawINI column, the structure of the INI file is as follows:

[BlockName1]
Key1=value
Key2=value1,value2,value3
Key3=
[BlockName1]
Key1=value
Key2=value1,value2,value3
Key3=

It is necessary to convert this text into the form of a new table, for example (maybe you can recommend another way?):

Block,Key,Value
--------------------------------------
BlockName1,Key1,value
BlockName1,Key2,value1,value2,value3
BlockName1,Key3
BlockName2,Key1,value
BlockName2,Key2,value1,value2,value3
BlockName2,Key3

I'm not familiar with T-SQL and I don't even know where to start solving this problem. How can this be solved? What features can help?

答案1

得分: 1

以下是您要求的代码部分的中文翻译:

作为示例:

DECLARE @TEXT VARCHAR(max) =
' [BlockName1]
Key1=value
Key2=value1,value2,value3
Key3=
[BlockName2]
Key1=value
Key2=value1,value2,value3
Key3=';

WITH
T AS
(
SELECT CONCAT('[', value) AS VAL, ROW_NUMBER() OVER(ORDER BY value) AS ORD
FROM STRING_SPLIT(@TEXT, '[')
WHERE value <> ''
),
S AS
(
SELECT ORD, LEFT(VAL, CHARINDEX(']', VAL)) AS BLOC,
SUBSTRING(VAL, CHARINDEX(']', VAL) + 3 , LEN(VAL) - CHARINDEX(']', VAL)) AS K
FROM T
)
SELECT ORD, BLOC, value AS CLE
FROM S
CROSS APPLY STRING_SPLIT(K, CHAR(10))
WHERE value <> '';

这是您要的翻译。如果您需要进一步的帮助,请告诉我。

英文:

As an example :

DECLARE @TEXT VARCHAR(max) = 
&#39;[BlockName1]
Key1=value
Key2=value1,value2,value3
Key3=
[BlockName2]
Key1=value
Key2=value1,value2,value3
Key3=&#39;;

WITH 
T AS
(
SELECT CONCAT(&#39;[&#39;, value) AS VAL, ROW_NUMBER() OVER(ORDER BY value) AS ORD
FROM STRING_SPLIT(@TEXT, &#39;[&#39;)
WHERE value &lt;&gt; &#39;&#39;
),
S AS
(
SELECT ORD, LEFT(VAL, CHARINDEX(&#39;]&#39;, VAL)) AS BLOC, 
       SUBSTRING(VAL, CHARINDEX(&#39;]&#39;, VAL) + 3 , LEN(VAL) - CHARINDEX(&#39;]&#39;, VAL)) AS K
FROM   T
)
SELECT ORD, BLOC, value AS CLE
FROM   S
       CROSS APPLY STRING_SPLIT(K, CHAR(10))
WHERE  value &lt;&gt; &#39;&#39;

That results :

ORD                  BLOC                   CLE
-------------------- ---------------------- ---------------------------
1                    [BlockName1]           Key1=value
1                    [BlockName1]           Key2=value1,value2,value3
1                    [BlockName1]           Key3=
2                    [BlockName2]           Key1=value
2                    [BlockName2]           Key2=value1,value2,value3
2                    [BlockName2]           Key3=

Thenyou can apply the same principe of STRING SPLIT (on coma) another time to extract values of keys

答案2

得分: 0

感谢@SQLPro的巨大帮助!我的问题的最终解决方案:

WITH 
[BLOCKS] AS
(
SELECT CONCAT(' [', value) AS [RAW]
FROM STRING_SPLIT(@rawINI, '[')
WHERE value <> ''
),
[PAIRS] AS
(
SELECT LEFT([RAW], CHARINDEX(']', [RAW])) AS [BLOCK], 
        SUBSTRING([RAW], CHARINDEX(']', [RAW]) + 3 , LEN([RAW]) - CHARINDEX(']', [RAW])) AS [PAIR]
FROM   [BLOCKS]
)

SELECT LTRIM(RTRIM([BLOCK])) AS [BLOCK],
        LTRIM(RTRIM(REPLACE(LEFT([KeyVal], CHARINDEX('=',[KeyVal])), '=', ''))) AS [KEY],
        LTRIM(RTRIM(REPLACE(SUBSTRING([KeyVal], CHARINDEX('=',[KeyVal]), LEN([KeyVal]) - CHARINDEX('=', [KeyVal])), '=', ''))) AS [VAL]
    FROM
    (
    SELECT [BLOCK], value AS [KeyVal]
    FROM  [PAIRS]
        CROSS APPLY STRING_SPLIT([PAIR], CHAR(10)) 
    WHERE  value <> ''
    ) AS RAWTABLE
WHERE  [KeyVal] <> ''

p.s. 我需要更多了解CROSS APPLY...

英文:

Thank you so much for @SQLPro great help! The final solution to my problem:

WITH 
[BLOCKS] AS
(
SELECT CONCAT(&#39;[&#39;, value) AS [RAW]
FROM STRING_SPLIT(@rawINI, &#39;[&#39;)
WHERE value &lt;&gt; &#39;&#39;
),
[PAIRS] AS
(
SELECT LEFT([RAW], CHARINDEX(&#39;]&#39;, [RAW])) AS [BLOCK], 
		SUBSTRING([RAW], CHARINDEX(&#39;]&#39;, [RAW]) + 3 , LEN([RAW]) - CHARINDEX(&#39;]&#39;, [RAW])) AS [PAIR]
FROM   [BLOCKS]
)

SELECT LTRIM(RTRIM([BLOCK])) AS [BLOCK],
		LTRIM(RTRIM(REPLACE(LEFT([KeyVal], CHARINDEX(&#39;=&#39;,[KeyVal])),&#39;=&#39;,&#39;&#39;))) AS [KEY],
		LTRIM(RTRIM(REPLACE(SUBSTRING([KeyVal], CHARINDEX(&#39;=&#39;,[KeyVal]), LEN([KeyVal]) - CHARINDEX(&#39;=&#39;, [KeyVal])),&#39;=&#39;,&#39;&#39;))) AS [VAL]
	FROM
	(
	SELECT [BLOCK], value AS [KeyVal]
	FROM  [PAIRS]
		CROSS APPLY STRING_SPLIT([PAIR], CHAR(10)) 
	WHERE  value &lt;&gt; &#39;&#39;
	) AS RAWTABLE
WHERE  [KeyVal] &lt;&gt; &#39;&#39;

p.s. i need to learn more about CROSS APPLY ...

huangapple
  • 本文由 发表于 2023年4月13日 16:49:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003486.html
匿名

发表评论

匿名网友

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

确定