tags:

views:

12272

answers:

2

Pretty simple question - I have an attribute that I would like to have double quotes in. How do I escape them? I've tried

  • \"
  • ""
  • \\"

And I've made the @xml variable both xml type and varchar(max) for all of them.

 declare @xml xml --(or varchar(max) tried both)

 set @xml = '<transaction><item value="hi "mom" lol" 
    ItemId="106"  ItemType="2"  instanceId="215923801"  dataSetId="1" /></transaction>'

 declare @xh int
 exec sp_xml_preparedocument @xh OUTPUT, @xml

 insert into @commits --I declare the table, just removed it for brevity
 select
    x.*
 from openxml(@xh,'/transaction/item')
  WITH (
    dataItemId int,
     dataItemType int,
    instanceId int,
    dataSetId int,
    value varchar(max)
  ) x
+15  A: 

Wouldn't that be &quot; in xml? i.e.

"hi &quot;mom&quot; lol"

**edit: ** tested; works fine:

declare @xml xml

 set @xml = '<transaction><item value="hi &quot;mom&quot; lol" 
    ItemId="106"  ItemType="2"  instanceId="215923801"  dataSetId="1" /></transaction>'

select @xml.value('(//item/@value)[1]','varchar(50)')
Marc Gravell
+1  A: 

tSql escapes a double quote with another double quote. So if you wanted it to be part of your sql string literal you would do this:

declare @xml xml 
set @xml = "<transaction><item value=""hi"" /></transaction>"

If you want to include a quote inside a value in the xml itself, you use an entity, which would look like this:

declare @xml xml
set @xml = "<transaction><item value=""hi &quot;mom&quot; lol"" /></transaction>"
Joel Coehoorn
Best not to use double-quotes as SQL string delimiters though. Single quotes are ANSI standard and always work, regardless of the QUOTED_IDENTIFIER setting.
bobince
Agreed, but I wanted to demonstrate that's it's possible, just in case there was any confusion about what he was trying to do.
Joel Coehoorn