Return 字符串的内容作为记录集

Return contents of string as set of records

我正在尝试从 Gravity Form list control 中提取数据。

本质上,列表控件允许您在 Web 表单(建立在 WordPress 网站上)上收集多列 table 数据。但是,table 和用户提供的记录都放在一行文本中,用于表单的记录提交。

我已经设法编写了一个自定义标量函数来获取原始输入文本,如下所示:

a:3:{i:0;a:3:{s:6:"ItemID";s:1:"1";s:4:"Name";s:10:"Dennis Dish";s:12:"Phone Number";s:12:"01225 123456";}i:1;a:3:{s:6:"ItemID";s:1:"2";s:4:"Name";s:13:"Sally Spatula";s:12:"Phone Number";s:12:"01225 654321";}i:2;a:3:{s:6:"ItemID";s:1:"3";s:4:"Name";s:10:"Terry Tins";s:12:"Phone Number";s:12:"01225 789456";}}

...和 ​​return 它是这样的(仍然是单个文本字符串):

ItemID: 1
Name: Dennis Dish
Phone Number: 01225 123456

ItemID: 2
Name: Sally Spatula
Phone Number: 01225 654321

ItemID: 3
Name: Terry Tins
Phone Number: 01225 789456

这相当简单且有用,但我现在需要编写一些东西来 return 将原始字符串改为一组记录。

所以,例如,如果我有一个 table 具有以下数据:

|   ID   |           RawString           |
|--------|-------------------------------|
|   1    | a:3:{i:0;a:3:{s:6:"ItemID"... |

..而不是 return 将其作为更整洁的字符串,如下所示:

|   ID   |           TidyString          |
|--------|-------------------------------|
|   1    | ItemID: 1 Name: Dennis Dish...|

..我正在寻找 return 类似以下内容的方法:

|   ID   | ItemID |     Name      | Phone Number | 
|--------|--------|---------------|--------------|
|   1    |   1    | Dennis Dish   | 01225 123456 |
|   1    |   2    | Sally Spatula | 01225 654321 |
|   1    |   3    | Terry Tins    | 01225 789456 |

在我的标量函数中,我能够识别有助于整理的原始字符串格式部分:

通过缩进原始字符串可能更容易形象化:

a:3:{
        i:0;
            a:3:{
                    s:2:"ItemID";
                    s:1:"1";

                    s:4:"Name";
                    s:10:"Dennis Dish";

                    s:12:"Phone Number";
                    s:12:"01225 123456";
                }
        i:1;
            a:3:{
                    s:2:"ItemID";
                    s:1:"2";

                    s:4:"Name";
                    s:13:"Sally Spatula";

                    s:12:"Phone Number";
                    s:12:"01225 654321";
                }

        i:2;
            a:3:{
                    s:2:"ItemID";
                    s:1:"3";

                    s:4:"Name";
                    s:10:"Terry Tins";

                    s:12:"Phone Number";
                    s:12:"01225 789456";
                }
    }

我不确定从哪里开始return将原始字符串中的数据作为结果集

如有任何指点,我们将不胜感激。

这是 NGrams8K 的工作。如果您下载该函数的副本,您可以这样做:

DECLARE @yourstring varchar(1000) =
'a:3:{
    i:0;
        a:3:{
                s:2:"ItemID";
                s:1:"1";

                s:4:"Name";
                s:10:"Dennis Dish";

                s:12:"Phone Number";
                s:12:"01225 123456";
            }
    i:1;
        a:3:{
                s:2:"ItemID";
                s:1:"2";

                s:4:"Name";
                s:13:"Sally Spatula";

                s:12:"Phone Number";
                s:12:"01225 654321";
            }

    i:2;
        a:3:{
                s:2:"ItemID";
                s:1:"3";

                s:4:"Name";
                s:10:"Terry Tins";

                s:12:"Phone Number";
                s:12:"01225 789456";
            }
    }';

WITH 
names(itemID, ps) AS -- extract the names
(
  SELECT
    ROW_NUMBER() OVER (ORDER BY position),
    SUBSTRING
    (
      @yourstring, 
      position+7, 
      CHARINDEX(';', @yourstring, position+7)-(position+8)
    )
  FROM dbo.NGrams8k(@yourstring, 7)
  WHERE token = '"Name";'
),
phoneNbrs(itemID, ps) AS -- extract the phone numbers
(
  SELECT
    ROW_NUMBER() OVER (ORDER BY position),
    SUBSTRING
    (
      @yourstring, 
      position+15, 
      CHARINDEX(';', @yourstring, position+15)-(position+16)
    )
  FROM dbo.NGrams8k(@yourstring, 15)
  WHERE token = '"Phone Number";'

)
SELECT 
 id = 1, 
 n.itemID,
 [Name]         = SUBSTRING(n.ps, CHARINDEX('"', n.ps)+1, 100),
 [Phone Number] = SUBSTRING(p.ps, CHARINDEX('"', p.ps)+1, 30)
FROM names n
JOIN phoneNbrs p ON n.itemID = p.itemID;

结果:

|   ID   | ItemID |     Name      | Phone Number | 
|--------|--------|---------------|--------------|
|   1    |   1    | Dennis Dish   | 01225 123456 |
|   1    |   2    | Sally Spatula | 01225 654321 |
|   1    |   3    | Terry Tins    | 01225 789456 |