sqlserver 读取xml 字符串方法

时间:2023-01-03 12:01:39

declare @xml xml
 declare @propertyName varchar(50)
 declare @str nvarchar(max)
   set @propertyName = 's_Code'
   set @xml = '<root> <row propertyName="s_Code">test</row><row propertyName="s_Posword">admin</row></root>'
  
   SELECT @str =@xml.value('(/root/row[@propertyName=sql:variable("@propertyName")])[1]', 'nvarchar(max)')
   select @str
  
  
 declare @xml xml
 declare @propertyName varchar(50)
 declare @str nvarchar(max)
   set @propertyName = 's_Posword'
   set @xml = '<root> <row propertyName="s_Code">test</row><row propertyName="s_Posword">admin</row></root>'  
   SELECT @str =@xml.value('(/root/row[@propertyName=sql:variable("@propertyName")])[1]', 'nvarchar(max)')
   select @str