I am a total noob with XQuery, but before at start digging deep into it, i'd like to ask some experts advice about whether i am looking at the correct direction.
I have XML in table that something looks like :
'<JOURNALEXT>
<JOURNAL journalno="1" journalpos="1" ledgercode="TD1">
</JOURNAL>
<JOURNAL journalno="1" journalpos="1" ledgercode="TD2">
</JOURNAL>
<JOURNAL journalno="1" journalpos="1" ledgercode="TD3">`enter code here`
</JOURNAL>
-----almost 50 such nodes
</JOURNALEXT>'
Now the ledger code attribute's value is there in some table. I have to filter all the nodes whose ledgercode value is not in the value that is there in table.
For example my ledger_code table has two entries TD1 & TD2
so I should get the resultant XML as
<JOURNALEXT>
<JOURNAL journalno="1" journalpos="1" ledgercode="TD3">
</JOURNAL>
-----almost 50 such nodes
</JOURNALEXT>
I can delete nodes based on one attribute by using.
declare @var_1 varchar(max) = 'TD1'
BEGIN TRANSACTION
update [staging_data_load].[TBL_STG_RAWXML_STORE] WITH (rowlock)
set XMLDATA.modify('delete /JOURNALEXT/JOURNAL[@ledgercode!= sql:variable("@var_1")]')
where job_id=@job_Id
but my case is quite complex..i need to get multiple ledgercodes from table and make sure only those nodes having table consisting ledgercodes remain. Rest all gets deleted.
I am using MS SQL SERVER 2012 ...as database and trying to write an xquery.