为什么 SQL 2005 在传递 sqlxml 参数时调用 CONVERT?

发布于 2024-07-28 02:57:48 字数 255 浏览 2 评论 0原文

我声明一个 SqlXml 参数并向其传递一个 XmlReader。 然后它被参数化并且 SQL 得到很好的执行。 但是,当查看通过探查器执行的 SQL 时,我注意到它采用 SqlXml 参数并对其调用 CONVERT...

CONVERT (xml, )

为什么服务器需要调用 CONVERT,我已经将其传递为有效xml? 是因为我没有定义架构吗?

我怀疑这会导致该查询性能不佳。 任何帮助都会很棒。

干杯

I am declaring an SqlXml param and passing it a XmlReader. This then gets parametrized and the SQL gets executed all very well. However, when looking at the SQL which gets executed through profiler I notice that it is taking the SqlXml parameter and calling CONVERT on it...

CONVERT (xml, )

Why does the server need to call CONVERT, I've already passed it valid xml? Is it because I'm not defining a schema?

I suspect this is causing bad performance on this query. Any help would be fantastic.

Cheers

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

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

发布评论

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

评论(1

沦落红尘 2024-08-04 02:58:34
    ...

        command.CommandText = "SELECT * FROM MySQLFunction(@xml)";

        List<int> Ids = new List<int>() { 1, 2, 3, 4, 5};

        using (MemoryStream stream = new MemoryStream())
        {
          using (XmlWriter writer = XmlWriter.Create(stream))
          {
            writer.WriteStartElement("Element");
            foreach (int i in Ids)
            {
              writer.WriteElementString("Id", i.ToString());
            }
            writer.WriteEndElement();
            writer.Flush();

            SqlXml sxml = new SqlXml(stream);

            SqlParameter param = new SqlParameter();
            param.ParameterName = "xml";
            param.Value = sxml;
            command.Parameters.Add(param);

            SqlDataReader reader = command.ExecuteReader();

    ...

MySQLFunction

SELECT Ids.Id.value('.','int') FROM @xml.nodes('/Element/Id') as Ids(Id)

当执行该函数时,SQL Server 在将其传递给函数之前调用 CONVERT (xml, '')。 所以看起来它作为字符串传递到 SQL Server,即使我已将其声明为 SqlXml...我希望它作为 xml 传递到 SQL Server 并且不需要转换?

干杯

    ...

        command.CommandText = "SELECT * FROM MySQLFunction(@xml)";

        List<int> Ids = new List<int>() { 1, 2, 3, 4, 5};

        using (MemoryStream stream = new MemoryStream())
        {
          using (XmlWriter writer = XmlWriter.Create(stream))
          {
            writer.WriteStartElement("Element");
            foreach (int i in Ids)
            {
              writer.WriteElementString("Id", i.ToString());
            }
            writer.WriteEndElement();
            writer.Flush();

            SqlXml sxml = new SqlXml(stream);

            SqlParameter param = new SqlParameter();
            param.ParameterName = "xml";
            param.Value = sxml;
            command.Parameters.Add(param);

            SqlDataReader reader = command.ExecuteReader();

    ...

MySQLFunction

SELECT Ids.Id.value('.','int') FROM @xml.nodes('/Element/Id') as Ids(Id)

When this gets executed, SQL Server calls CONVERT (xml, '') before passing it to the function. So it looks like it gets passed as a string to SQL Server even though I've declared it as SqlXml... I would have expected it to be passed to SQL Server as xml and not conversion would be needed?

Cheers

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