主题SQL Server XML modify with no result,我收到错误
XQuery [modify()]: Only non-document nodes can be inserted. Found "xs:string ?".
新节点的主体:
<formant id="'+lower(newid())+'" name="_name" width="155" height="20" x="125" y="761"
tabindex="0" source-name="_sn" readonly="false" required="false"
font="_fnt"><value></value></formant>
任何想法如何解决?
DECLARE @newNodeData varchar(max) = <formant id="'+lower(newid())+'" name="_name" width="155" height="20" x="125" y="761"
tabindex="0" source-name="_sn" readonly="false" required="false"
font="_fnt"><value></value></formant>
...对@newNodeData变量的一些文本修改
然后
declare @xml xml;
select @xml = t.xmlData //t.xmlData is ntext type
from table t
where id=123
set @xml.modify('declare namespace xsd="http://www.cebip.pl/xml/schematy/formularze/v1.0/definicja-formularza.xsd"; insert sql:variable("@newNodeData") as first into (/xsd:form-definition/xsd:page)[1]')
update table
set xmlData = @newNodeData
where id=123
答案 0 :(得分:6)
如果要进行文本修改,请在varchar(max)
上执行这些修改,然后将结果存储在最终的XML变量中,然后在modify()
DECLARE @newNodeDataTmp varchar(max) = '<formant id="'+lower(newid())+'" name="_name" width="155" height="20" x="125" y="761"
tabindex="0" source-name="_sn" readonly="false" required="false"
font="_fnt"><value></value></formant>'
-- Modifications
declare @newNodeData xml = cast(@newNodeDataTmp as xml)
您在modify()
中使用的变量需要是XML类型。