3

I have the following Code,

@MasterXML= '
    <Report MustUnderstand="df" xmlns="http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner" xmlns:df="http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition/defaultfontfamily">
      <DataSets>
        <DataSet Name="'+@Datasetname+'">
          <Query>
          </Query>
          <Fields>
          <Field>
          </Field>
          </Fields>
        </DataSet>
      </DataSets>
      </Report> '
    WHILE @i <= @Cnt
    BEGIN

    Select @xml=
     (  
      SELECT  @cname AS [@Name]
               ,@cname AS [DataField]
               ,'System.String' AS [rd:TypeName]
        FOR XML PATH('Field')
    );
    SET @i = @i + 1
    SET @MasterXML.modify(' insert sql:variable("@xml") as last into  (Fields)[1] ' )  
    end
    select @MasterXML

I am Generating a XMLat @xml through loop and i want to insert it after

I tried SET @MasterXML.modify(' insert sql:variable("@xml") as last into (Fields)[1] ' ), but the generated fields does not add up.

Can Anyone help me out?

1
  • I'm pretty sure, that this can be done much better... You do not tell us, where the @cname is coming from, but this can be done without any loop or .modify() - at least I think so... My suggestion: There are some answers upon this question. If they solve it, please accept the one you like most and use your right to vote. Then set up a new question with some more information what you really try to achieve. This sounds like an xy-problem Commented Mar 28, 2018 at 7:11

2 Answers 2

1

@MasterXML has a default namespace http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition so you need to reference the nodes using that namespace.

set @MasterXML.modify('
  declare default element namespace "http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition";
  insert sql:variable("@xml") as last into  (/Report/DataSets/DataSet/Fields)[1]');

Note that the elements in the XML you are adding to @MasterXML does not use a namespace so you will get xmlns="" added for you for those elements. If you want them to be part of the same namespace you have to specify that in your for xml query.

Add Namespaces to Queries with WITH XMLNAMESPACES

Sign up to request clarification or add additional context in comments.

Comments

0

Your xpath expression isn't valid. try something like this:

SET @MasterXML.modify(' insert sql:variable("@xml") as last into  (/Report/DataSets/DataSet/Fields)[1] ' )  

2 Comments

What do you mean "the fields does not add up"?
The @XML part does not adds up to specified node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.