Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Updating the xml to be returned from stored procedure

Updating the xml to be returned from stored procedure

Scheduled Pinned Locked Moved Database
databasexmlhelptutorialannouncement
10 Posts 2 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    ONeil Tomlinson
    wrote on last edited by
    #1

    Hi i want to update the "name" element in XML using the OPENXML in SQLServer. below is what i'm trying to do without success. Im not even sure if its possible. The value "xxxxx" is normally the name of a table, but when dealing with xml i'm not sure what should be there. Im passing in the xml as a parameter to the stored procedure and doing suff to it. but the last thing in the stored procedure is to update the "name" element. not sure how to do this need some help please. Thanks ONeil update xxxxxxx set OPENXML (@hDoc, '/Employees/Employee') WITH (tName varchar(20) './Name') = 'UpdatedName'

    W 1 Reply Last reply
    0
    • O ONeil Tomlinson

      Hi i want to update the "name" element in XML using the OPENXML in SQLServer. below is what i'm trying to do without success. Im not even sure if its possible. The value "xxxxx" is normally the name of a table, but when dealing with xml i'm not sure what should be there. Im passing in the xml as a parameter to the stored procedure and doing suff to it. but the last thing in the stored procedure is to update the "name" element. not sure how to do this need some help please. Thanks ONeil update xxxxxxx set OPENXML (@hDoc, '/Employees/Employee') WITH (tName varchar(20) './Name') = 'UpdatedName'

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      Have a look at XML DML and especially updating a value: replace value of (XML DML)[^] Hope this helps, Mika

      The need to optimize rises from a bad design

      O 1 Reply Last reply
      0
      • W Wendelius

        Have a look at XML DML and especially updating a value: replace value of (XML DML)[^] Hope this helps, Mika

        The need to optimize rises from a bad design

        O Offline
        O Offline
        ONeil Tomlinson
        wrote on last edited by
        #3

        Thanks that helped alot. I manage to get cross that hurdle but now im faced with another problem. im getting error --- "The argument 1 of the xml data type method "modify" must be a string literal" --- for the below statement. I want to take the parameter @code and make it part of the insert statement. if i hardcode a value in the insert statement it work but once i reference the "@code" im getting "The argument 1 of the xml data type method "modify" must be a string literal". Any help would be appreciated ########## XML passing IN ############### <Employees> <Employee ID= '111'><Code>1</Code><Name>Name1</Name></Employee> </Employees> ############# Stored Procedure ########### ALTER PROC [dbo].[sp_Testing] @empdata xml, @code varchar(255) AS DECLARE @hDoc int exec sp_xml_preparedocument @hDoc OUTPUT,@empdata Select @empdata set @empdata.modify('insert <Employee ID= ''101''>'+@code+'</Code><Name>''Name101''</Name></Employee>into (/Employees)[1]') --return XML Select @empdata

        W 1 Reply Last reply
        0
        • O ONeil Tomlinson

          Thanks that helped alot. I manage to get cross that hurdle but now im faced with another problem. im getting error --- "The argument 1 of the xml data type method "modify" must be a string literal" --- for the below statement. I want to take the parameter @code and make it part of the insert statement. if i hardcode a value in the insert statement it work but once i reference the "@code" im getting "The argument 1 of the xml data type method "modify" must be a string literal". Any help would be appreciated ########## XML passing IN ############### <Employees> <Employee ID= '111'><Code>1</Code><Name>Name1</Name></Employee> </Employees> ############# Stored Procedure ########### ALTER PROC [dbo].[sp_Testing] @empdata xml, @code varchar(255) AS DECLARE @hDoc int exec sp_xml_preparedocument @hDoc OUTPUT,@empdata Select @empdata set @empdata.modify('insert <Employee ID= ''101''>'+@code+'</Code><Name>''Name101''</Name></Employee>into (/Employees)[1]') --return XML Select @empdata

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          Would this piece code perform the operation correctly? If I understood correctly, you want to insert an element to the xml data so you don't need to dynamically parse or concatenate the string.

          declare @empdata xml
          set @empdata = '<Employees><Employee ID= ''111''><Code>1</Code><Name>Name1</Name></Employee></Employees>'
          Select @empdata
          set @empdata.modify('insert <Employee ID= ''101''></Employee> into (/Employees)[1]')
          Select @empdata

          The result:

          <Employees>
          <Employee ID="111">
          <Code>1</Code>
          <Name>Name1</Name>
          </Employee>
          <Employee ID="101" />
          </Employees>

          Mika

          The need to optimize rises from a bad design

          O 1 Reply Last reply
          0
          • W Wendelius

            Would this piece code perform the operation correctly? If I understood correctly, you want to insert an element to the xml data so you don't need to dynamically parse or concatenate the string.

            declare @empdata xml
            set @empdata = '<Employees><Employee ID= ''111''><Code>1</Code><Name>Name1</Name></Employee></Employees>'
            Select @empdata
            set @empdata.modify('insert <Employee ID= ''101''></Employee> into (/Employees)[1]')
            Select @empdata

            The result:

            <Employees>
            <Employee ID="111">
            <Code>1</Code>
            <Name>Name1</Name>
            </Employee>
            <Employee ID="101" />
            </Employees>

            Mika

            The need to optimize rises from a bad design

            O Offline
            O Offline
            ONeil Tomlinson
            wrote on last edited by
            #5

            It works fine if i hardcode the values in (as i said in my previous post and as you have done in your previous post). the problem is when i try passing in the "@code varchar(255)" as a parameter and use it to build the insert statement. that when im getting the error

            W 1 Reply Last reply
            0
            • O ONeil Tomlinson

              It works fine if i hardcode the values in (as i said in my previous post and as you have done in your previous post). the problem is when i try passing in the "@code varchar(255)" as a parameter and use it to build the insert statement. that when im getting the error

              W Offline
              W Offline
              Wendelius
              wrote on last edited by
              #6

              Sorry about the misunderstanding, that's what you said in the first place (I'll have to go back to the english lessons and learn how to read :doh: ). Let's try something else. Note that the variable for the element to add is not varchar but xml (this is required).

              declare @empdata xml
              declare @somethingToAdd xml
              set @empdata = '<Employees><Employee ID= ''111''><Code>1</Code><Name>Name1</Name></Employee></Employees>'
              set @somethingToAdd = '<Employee ID= ''101''></Employee>'
              Select @empdata
              set @empdata.modify('insert sql:variable("@somethingToAdd") into (/Employees)[1]')
              Select @empdata

              Would this help?

              The need to optimize rises from a bad design

              O 1 Reply Last reply
              0
              • W Wendelius

                Sorry about the misunderstanding, that's what you said in the first place (I'll have to go back to the english lessons and learn how to read :doh: ). Let's try something else. Note that the variable for the element to add is not varchar but xml (this is required).

                declare @empdata xml
                declare @somethingToAdd xml
                set @empdata = '<Employees><Employee ID= ''111''><Code>1</Code><Name>Name1</Name></Employee></Employees>'
                set @somethingToAdd = '<Employee ID= ''101''></Employee>'
                Select @empdata
                set @empdata.modify('insert sql:variable("@somethingToAdd") into (/Employees)[1]')
                Select @empdata

                Would this help?

                The need to optimize rises from a bad design

                O Offline
                O Offline
                ONeil Tomlinson
                wrote on last edited by
                #7

                Im feeling very stupid today. for some reason im always getting errors. copy, paste and try the below code. It parses ok but when Execute im getting this error ------- "The data types varchar and xml are incompatible in the add operator." ######################################## ALTER PROC [dbo].[sp_Testing2] @empdata xml, @code varchar AS DECLARE @hDoc int declare @somethingToAdd xml exec sp_xml_preparedocument @hDoc OUTPUT,@empdata set @somethingToAdd = '<Employee ID= ''101''><Code>'+@code+'</Code><Name>''Name101''</Name></Employee>' Select @empdata set @empdata.modify('insert sql:variable('+ @somethingToAdd+') into (/Employees)[1]') -- return the XMl Select @empdata ####################################################

                W 1 Reply Last reply
                0
                • O ONeil Tomlinson

                  Im feeling very stupid today. for some reason im always getting errors. copy, paste and try the below code. It parses ok but when Execute im getting this error ------- "The data types varchar and xml are incompatible in the add operator." ######################################## ALTER PROC [dbo].[sp_Testing2] @empdata xml, @code varchar AS DECLARE @hDoc int declare @somethingToAdd xml exec sp_xml_preparedocument @hDoc OUTPUT,@empdata set @somethingToAdd = '<Employee ID= ''101''><Code>'+@code+'</Code><Name>''Name101''</Name></Employee>' Select @empdata set @empdata.modify('insert sql:variable('+ @somethingToAdd+') into (/Employees)[1]') -- return the XMl Select @empdata ####################################################

                  W Offline
                  W Offline
                  Wendelius
                  wrote on last edited by
                  #8

                  One problem is that you modified the insert literal (don't concatenate!). If you don't need hDoc in somewhere else, the procedure would be in minimum something like this (note that @code is xml):

                  ALTER PROC [dbo].[sp_Testing2]
                  @empdata xml,
                  @code xml
                  AS
                  Select @empdata
                  set @empdata.modify('insert sql:variable("@code") into (/Employees)[1]')
                  -- return the XMl
                  Select @empdata

                  The need to optimize rises from a bad design

                  O 1 Reply Last reply
                  0
                  • W Wendelius

                    One problem is that you modified the insert literal (don't concatenate!). If you don't need hDoc in somewhere else, the procedure would be in minimum something like this (note that @code is xml):

                    ALTER PROC [dbo].[sp_Testing2]
                    @empdata xml,
                    @code xml
                    AS
                    Select @empdata
                    set @empdata.modify('insert sql:variable("@code") into (/Employees)[1]')
                    -- return the XMl
                    Select @empdata

                    The need to optimize rises from a bad design

                    O Offline
                    O Offline
                    ONeil Tomlinson
                    wrote on last edited by
                    #9

                    Thanks you have helped alot

                    W 1 Reply Last reply
                    0
                    • O ONeil Tomlinson

                      Thanks you have helped alot

                      W Offline
                      W Offline
                      Wendelius
                      wrote on last edited by
                      #10

                      You're welcome :)

                      The need to optimize rises from a bad design

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups