Variables in TSQL XML
How to use variables in TSQL XML methods
In order to search XML in TSQL using a variable, use the sql:variable("@VAR")
format...
DECLARE @MYVAR INT = 1
SELECT * FROM [MYTABLE] WHERE [XMLCOL].exist('/data/item=sql:variable("@MYVAR")') = 1
If you're using the modify
method, then you need to include it within curly braces...
DECLARE @MYVAR INT = 1
UPDATE [MYTABLE] SET [XMLCOL].modify('insert <item>{sql:variable("@MYVAR")}</item> as last into /data[1]')
Added 14/04/2021 17:08