Usually I use a XML variable in filters, because they are easy to work.
Considering tablea, where column1 is the primary key:
declare @xml xml = '<column1>1</column1><column1>2</column1>'
select *
from tablea
where
column1 in (select x.i.value('.', 'bigint') from @xml.nodes('/column1') x(i)) or
@xml is null
It works because the select over the xml returns two rows, with values 1 and 2.
Now I have a tableb, with a composite primary key, column1 and column2. So:
declare @xml xml = '<row><column1>1</column1><column2>2</column2></row><row><column1>3</column1><column2>4</column2></row>'
How can I write a select over the xml to return each row and columns, like:
column1 column2
1 2
3 4
declare @xml xml = '<row><column1 a="a">1</column1><column2>2</column2></row><row>' +
'<column1>3</column1><column2>4</column2></row>'
select col.value('data(column1[1])', 'int') as column1
, col.value('data(column2[1])', 'int') as column2
from @xml.nodes('/row') tbl(col)
Example at SQL Fiddle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With