Use of xml.modify to insert parameters into specific element of an xml column

前端 未结 3 1173
长发绾君心
长发绾君心 2020-12-15 09:28

I would like to use a stored procedure to insert some values passed in as parameters into elements in the xml of a column. I have this so far The following parameters:

3条回答
  •  离开以前
    2020-12-15 10:02

    Not sure I fully understand your logic, but you can insert into xml using sql:variable with curly brackets eg

    DECLARE @profiles_xml xml
    
    set @profiles_xml = '
      20
      
        BC4A18CA-AFB5-4268-BDA9-C990DAFE7783
        somename
        
          
             activity1
          
        
      
    '
    
    
    SELECT 'before' s, DATALENGTH(@profiles_xml) dl, @profiles_xml
    
    DECLARE @user_id CHAR(36), @activity_name NVARCHAR(MAX), @display_name NVARCHAR(MAX)
    SELECT @user_id = 'BC4A18CA-AFB5-4268-BDA9-C990DAFE7783', @activity_name = 'TEST ACTIVITY NAME', @display_name = 'TEST DISPLAY NAME'
    
    SET @profiles_xml.modify('
        insert {sql:variable("@activity_name")}{sql:variable("@display_name")}
        as first
        into (/Profile/User/Activities)[1]')
    
    SELECT 'after' s, DATALENGTH(@profiles_xml) dl, @profiles_xml
    

提交回复
热议问题