简体   繁体   English

MSSQL XQuery的语法错误或根本没有结果

[英]Syntax error of MSSQL XQuery or no results at all

I used this white paper from MS for reference: http://msdn.microsoft.com/en-us/library/ms345117(v=sql.90).aspx 我使用了MS的这份白皮书作为参考: http : //msdn.microsoft.com/zh-cn/library/ms345117(v=sql.90).aspx

However I get an error when I attempt the following query: 但是,当我尝试以下查询时出现错误:

    command = new SqlCommand(
    "SET QUOTED_IDENTIFIER ON SELECT XML FROM XMLObject WHERE" + 
    "XML.exist('//Root/ActiveRecord[" + s1 + 
    "= " + s2 + "]') = 1"
    , connection);

The error is: "Syntax error near ".". 错误为:“。附近的语法错误。”。

I am able to execute the command if I remove everything after "XMLObject" until the comma, and something IS returned if elements exist in the DB. 如果删除“ XMLObject”之后的所有内容,直到逗号,则可以执行该命令;如果数据库中存在元素,则返回某些IS。

I am also curious as to whether I am setting the QUOTED_IDENTIFIER value the correct way. 我也对我是否以正确的方式设置QUOTED_IDENTIFIER值感到好奇。

Any example projects/tutorials in C#/MSSQL I might look at would be appreciated as well! 我可能会看的任何C#/ MSSQL示例项目/教程也将不胜感激!

EDIT: For anyone seeing this, I have also found that the correct way of using exist is: 编辑:对于任何看到这一点的人,我还发现存在的正确使用方法是:

    "...exist('/Root/ActiveRecord[" + s1 + 
    "= \"" + s2 + "\"]') = 1"

Without the "s only numerical comparisons work. 如果没有,则只能进行数字比较。

You should avoid building SQL by concatenating strings at it is SQL injection vulnerable. 您应该避免通过串联字符串构建SQL,因为它容易受到SQL注入的攻击。 You need a semi-colon after ON. 开启后需要分号。 Your columns datatype must be xml or you must convert it to xml. 您的column数据类型必须为xml或必须将其转换为xml。

I think you are missing a space between WHERE and XML : 我认为您在WHEREXML之间缺少空格:

command = new SqlCommand(
    "SET QUOTED_IDENTIFIER ON SELECT XML FROM XMLObject WHERE " + // Note space
    "XML.exist('//Root/ActiveRecord[" + s1 + 
    "= " + s2 + "]') = 1"
    , connection);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM