嗨,当我试图添加属性到子节点时,我正在获得字符串文字错误。我如何修改我的代码,以便成功地添加一个属性。
declare @count int=(select mxGraphXML.value('count(/mxGraphModel/root/Cell/@Value )','nvarchar') from TABLE_LIST
where Table_ListID=1234 )
declare @index int=1;
while @index<=@count
begin
declare @Value varchar(100)= @graphxml.value('(/mxGraphModel/root/Cell/@Value )[1]','nvarchar');
SET @graphxml.modify('insert attribute copyValueID {sql:variable("@Value ")}
as first into (/mxGraphModel/root/Cell)['+convert(varchar,@index)+']');
end
set @index=@index+1;
end
您在应该使用CONCAT函数的地方使用了加法运算符。所以
'insert attribute copyValueID {sql:variable("@Value ")}
as first into (/mxGraphModel/root/Cell)['+convert(varchar,@index)+']'
被强制转换为一个数字。试一试:
CONCAT('insert attribute copyValueID {sql:variable("@Value ")}
as first into (/mxGraphModel/root/Cell)[',convert(varchar,@index),']')
。
Adam,你可以在Microsoft T-SQL中这样做:
declare @sql nvarchar(max)
set @sql = 'set @myxml.modify(''
insert (
attribute scalableFieldId {sql:variable("@sf_id")},
attribute myTypeId {sql:variable("@my_type_id")}
) into (/VB/Condition/Field[@fieldId=sql:variable("@field_id")
and @fieldCode=sql:variable("@field_code")])['+
cast(@instance as varchar(3))+']'')'
exec sp_executesql
@sql
,N'@myxml xml output, @field_code varchar(20),
@field_id varchar(20), @sf_id int, @my_type_id tinyint'
,@myxml = @myxml output
,@field_code = @field_code
,@field_id = @field_id
,@sf_id = @sf_id
,@my_type_id = @my_type_id
看到我做的了吗?这只是动态SQL的一个聪明的使用,以克服微软的"字符串字面错误"的愚蠢限制。
重要提示:是的,你可以通过在某些地方使用sql:variable()来实现这一点,但是祝你好运,在方括号内的节点号限定符中使用它!如果没有动态SQL,就无法做到这一点!
这个技巧实际上不是我的,我是在把头撞到墙上一段时间后从https://www.opinionatedgeek.com/Snaplets/Blog/Form/Item/000299/Read得到这个主意的。
如果我的样本不工作或某些东西不清楚,请随时提问。