在 SQL Server 2005 中解析查询字符串

发布于 2024-12-28 18:43:36 字数 287 浏览 0 评论 0原文

在 SQL Server 中是否有一种简单的方法来解析 QueryString 参数(例如 foo=bar&temp=baz)?

我最终需要的是一个带有名称/值对的“表”。

| foo  | bar |
| temp | baz |

虽然在上面的示例中这很简单,但如果字符串开始包含转义字符(例如 %3D),就会变得更困难,并且当涉及 UTF-8 时甚至会更困难。

有现有的解决方案吗? SQL Server 的 URLDecode 实现将是人间天堂。

Is there a simple way to parse QueryString parameters (e.g. foo=bar&temp=baz) in SQL Server?

What I need in the end is a "table" with the name/value pairs.

| foo  | bar |
| temp | baz |

While it would be simple in the above example it becomes harder if the strings start to contain escaped characters (e.g. %3D) and even tougher when UTF-8 is involved.

Any existing solutions? An implementation of URLDecode for SQL Server would be heaven on earth.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风向决定发型 2025-01-04 18:43:36

因此,要解析查询字符串,只需在函数中使用 CTE 即可。这是代码。

CREATE FUNCTION dbo.SplitQueryString (@s varchar(8000))
RETURNS table
AS
RETURN (
    WITH splitter_cte AS (
      SELECT CHARINDEX('&', @s) as pos, 0 as lastPos
      UNION ALL
      SELECT CHARINDEX('&', @s, pos + 1), pos
      FROM splitter_cte
      WHERE pos > 0
      ),
    pair_cte AS (
    SELECT chunk,
           CHARINDEX('=', chunk) as pos
    FROM (
        SELECT SUBSTRING(@s, lastPos + 1,
                         case when pos = 0 then 80000
                         else pos - lastPos -1 end) as chunk
        FROM splitter_cte) as t1
  )
    SELECT substring(chunk, 0, pos) as keyName,
           substring(chunk, pos+1, 8000) as keyValue
    FROM pair_cte
)
GO

declare @queryString varchar(2048)
set @queryString = 'foo=bar&temp=baz&key=value';
SELECT *
  FROM dbo.SplitQueryString(@queryString)
OPTION(MAXRECURSION 0);

运行时会产生以下输出。

keyName  keyValue
-------  --------
foo      bar
temp     baz
key      value
(3 row(s) affected)

我相信这将完全满足您的要求。

So to parse the query string, just use a CTE in a function. Here is the code.

CREATE FUNCTION dbo.SplitQueryString (@s varchar(8000))
RETURNS table
AS
RETURN (
    WITH splitter_cte AS (
      SELECT CHARINDEX('&', @s) as pos, 0 as lastPos
      UNION ALL
      SELECT CHARINDEX('&', @s, pos + 1), pos
      FROM splitter_cte
      WHERE pos > 0
      ),
    pair_cte AS (
    SELECT chunk,
           CHARINDEX('=', chunk) as pos
    FROM (
        SELECT SUBSTRING(@s, lastPos + 1,
                         case when pos = 0 then 80000
                         else pos - lastPos -1 end) as chunk
        FROM splitter_cte) as t1
  )
    SELECT substring(chunk, 0, pos) as keyName,
           substring(chunk, pos+1, 8000) as keyValue
    FROM pair_cte
)
GO

declare @queryString varchar(2048)
set @queryString = 'foo=bar&temp=baz&key=value';
SELECT *
  FROM dbo.SplitQueryString(@queryString)
OPTION(MAXRECURSION 0);

when run produces the following output.

keyName  keyValue
-------  --------
foo      bar
temp     baz
key      value
(3 row(s) affected)

I believe that this will do exactly what you are asking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文