SQL 大写 INSERT 的 XPath 因语法错误而失败

SQL XPath with upper case INSERT fails with syntax error

我有一个 SQL 服务器查询,用于将 XML 节点插入某些 XML;当我使用大写字母 INSERT 时会出现语法错误,但小写字母 insert 有效。

UPDATE @xTABLE
SET x.modify('
INSERT <newthing>I am here</newthing>
as last
into (/rootthing/InsertInMe)[1]
'); 

这里的例子都是大写的,为什么会失败并出现语法错误:

Msg 2209, Level 16, State 1, Line 25 XQuery [@xTABLE.x.modify()]: Syntax error near 'I'

运行 这在 SSMS 中。

作为参考,示例中大写:https://docs.microsoft.com/en-us/sql/t-sql/xml/insert-xml-dml?view=sql-server-ver15

这是一些示例 SQL,其中包含工作(第一个)和失败(第二个)更新:

-- setup some sample data:
DECLARE @xtable AS TABLE (
     x xml
);

DECLARE @x AS XML;
SET @x = '
<rootthing>
  <InsertInMe>
  </InsertInMe>
</rootthing>
';
INSERT INTO @xtable (x) Values(@x);

SELECT x AS xBefore
FROM @xtable;

-- this works
UPDATE @xTABLE
SET x.modify('
insert <newthing>I am here</newthing>
as last
into (/rootthing/InsertInMe)[1]
');

-- this gives a syntax error
UPDATE @xTABLE
SET x.modify('
INSERT <newthing>I am here</newthing>
as last
into (/rootthing/InsertInMe)[1]
');

SELECT x AS xAfter
FROM @xtable; 

XQuery 区分大小写。 INSERT <> insert。这在 XQuery documentation (which SQL Server follows per XQuery Language Reference (SQL Server)) 第 2 节“基础知识”中有所说明。

Like XML, XQuery is a case-sensitive language. Keywords in XQuery use lower-case characters and are not reserved—that is, names in XQuery expressions are allowed to be the same as language keywords, except for certain unprefixed function-names listed in A.3 Reserved Function Names.

因此,INSERT 不是可识别的语法,因为它不是 insert