在存储过程中使用 XML 时出错(PDO + MS SQL 2008)

发布于 2024-10-28 08:14:03 字数 2037 浏览 1 评论 0原文

当我使用 SQL Profiler 时,我会看到来自 Debian 服务器的所有请求,并且可以执行它们而不会出现任何错误。当我从 Apache/PHP + PDO 执行 SP 时,出现以下错误:

SQLSTATE[HY000]:一般错误:1934 一般 SQL Server 错误:检查 来自 SQL Server 的消息 [1934] (严重性 16)[(null)]

事实上,错误没有正确显示。真正的错误应该是这样的:

消息 1934,级别 16,状态 1,程序 CALCUL_NOTATION_XML2,第 38 行选择 失败,因为以下 SET 选项设置不正确: 'CONCAT_NULL_YIELDS_NULL, ANSI_警告,ANSI_PADDING'。核实 SET 选项的使用正确 带有索引视图和/或索引 计算列和/或过滤列 索引和/或查询通知 和/或 XML 数据类型方法和/或 空间索引操作。

我的 SP 看起来像这样:

CREATE PROCEDURE dbo.TEST_XML(
    @MyXML XML
)
AS
BEGIN
    SET @MyXML = '<SampleXML>
    <Colors>
        <Color1>White</Color1>
    </Colors>
    <Fruits>
        <Fruits1>Apple</Fruits1>
    </Fruits>
    </SampleXML>'

    SELECT
    a.b.value('Colors[1]/Color1[1]','varchar(10)') AS Color1,
    a.b.value('Fruits[1]/Fruits1[1]','varchar(10)') AS Fruits1,
    FROM @MyXML.nodes('SampleXML') a(b) 
END

该错误仅因 PDO 连接到数据库的方式而发生。它使用以下选项:

-- network protocol: TCP/IP
set quoted_identifier off
set arithabort off
set numeric_roundabort off
set ansi_warnings off
set ansi_padding off
set concat_null_yields_null off
set ansi_nulls off
set cursor_close_on_commit off
set implicit_transactions off
set language us_english
set dateformat mdy
set datefirst 7
set transaction isolation level read committed

我想知道是否可以更改 PDO 中的登录设置,使其看起来更像:

set quoted_identifier on
set arithabort on
set numeric_roundabort off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set cursor_close_on_commit off
set implicit_transactions off
set language Français
set dateformat dmy
set datefirst 1
set transaction isolation level read committed

我已阅读 site,一种解决方法,但我想知道是否有真正的解决方案(就像配置 PDO I不知道如何让它发挥作用)。

When I use SQL Profiler, I see all the requests coming from the Debian server and I can execute them without having any error. When I execute the SP from a Apache/PHP + PDO I get the following error:

SQLSTATE[HY000]: General error: 1934
General SQL Server error: Check
messages from the SQL Server [1934]
(severity 16) [(null)]

In fact the error is not shown properly. The real error should be like the following:

Msg 1934, Level 16, State 1, Procedure
CALCUL_NOTATION_XML2, Line 38 SELECT
failed because the following SET
options have incorrect settings:
'CONCAT_NULL_YIELDS_NULL,
ANSI_WARNINGS, ANSI_PADDING'. Verify
that SET options are correct for use
with indexed views and/or indexes on
computed columns and/or filtered
indexes and/or query notifications
and/or XML data type methods and/or
spatial index operations.

My SP looks something like this:

CREATE PROCEDURE dbo.TEST_XML(
    @MyXML XML
)
AS
BEGIN
    SET @MyXML = '<SampleXML>
    <Colors>
        <Color1>White</Color1>
    </Colors>
    <Fruits>
        <Fruits1>Apple</Fruits1>
    </Fruits>
    </SampleXML>'

    SELECT
    a.b.value('Colors[1]/Color1[1]','varchar(10)') AS Color1,
    a.b.value('Fruits[1]/Fruits1[1]','varchar(10)') AS Fruits1,
    FROM @MyXML.nodes('SampleXML') a(b) 
END

The error only happens because of the way PDO connect to the database. It use the following options:

-- network protocol: TCP/IP
set quoted_identifier off
set arithabort off
set numeric_roundabort off
set ansi_warnings off
set ansi_padding off
set concat_null_yields_null off
set ansi_nulls off
set cursor_close_on_commit off
set implicit_transactions off
set language us_english
set dateformat mdy
set datefirst 7
set transaction isolation level read committed

I wonder if it would be possible to change the login setup in PDO to have something that looks more like:

set quoted_identifier on
set arithabort on
set numeric_roundabort off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set cursor_close_on_commit off
set implicit_transactions off
set language Français
set dateformat dmy
set datefirst 1
set transaction isolation level read committed

I have read on a site, a kind of workaround, but I wonder if there is a real solution (like configuring PDO I don't know how to make it work).

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文