XML-Handhabung, wie man überprüft, ob ein Attribut in einem Element in SQL server enthalten ist

folgend ist die Beispiel-XML-String:

<R RMA="1" UsrID="AXxxx" FirstName="xx" LastName="yy" Email="dd" title=""> <Contract ContractNr="xxx1" ETC="" TermRC="" DESC="" INVCOMMENT="" TermDate="03/01/2014" PO="XXX" DestRule="xxxx"> <Asset ID="12345" AXID="abcde" RecvdDt="05/02/2014" ReconDt="05/02/2014" ReceiptID=""/> <Asset ID="67890" AXID="abcde1" RecvdDt="05/02/2014" ReconDt="05/02/2014" ReceiptID=""/> </Contract> <Contract ContractNr="xxx2" ETC="" TermRC="" DESC="" INVCOMMENT="" TermDate="03/01/2014" DestRule="xxxx"> <Asset ID="54321" AXID="edcba" RecvdDt="05/02/2014" ReconDt="05/02/2014" ReceiptID="" /> <Asset ID="09876" AXID="edcba1" RecvdDt="05/02/2014" ReconDt="05/02/2014" ReceiptID="" /> </Contract> <Contract ContractNr="xxx1" ETC="" TermRC="" DESC="" INVCOMMENT="" TermDate="03/01/2014" PO="XXX" > <Asset ID="12345" AXID="abcde" RecvdDt="05/02/2014" ReconDt="05/02/2014" ReceiptID=""/> <Asset ID="67890" AXID="abcde1" RecvdDt="05/02/2014" ReconDt="05/02/2014" ReceiptID=""/> </Contract> <Contract ContractNr="xxx1" ETC="" TermRC="" DESC="" INVCOMMENT="" TermDate="03/01/2014" > <Asset ID="12345" AXID="abcde" RecvdDt="05/02/2014" ReconDt="05/02/2014" ReceiptID=""/> <Asset ID="67890" AXID="abcde1" RecvdDt="05/02/2014" ReconDt="05/02/2014" ReceiptID=""/> </Contract> </R> 

Ich möchte überprüfen, ob alle Elemente PO- und DestRule-Attribute enthalten, falls nicht, falsch drucken, andernfalls drucken true.

Kann SQL-server tun, wie man die SQL-statement zu schreiben?

Versuche dies:

 DECLARE @xml xml = '<R RMA="1" UsrID="AXxxx" FirstName="xx" LastName="yy" Email="dd" title=""> <Contract ContractNr="xxx1" ETC="" TermRC="" DESC="" INVCOMMENT="" TermDate="03/01/2014" PO="XXX" DestRule="xxxx"> <Asset ID="12345" AXID="abcde" RecvdDt="05/02/2014" ReconDt="05/02/2014" ReceiptID=""/> <Asset ID="67890" AXID="abcde1" RecvdDt="05/02/2014" ReconDt="05/02/2014" ReceiptID=""/> </Contract> <Contract ContractNr="xxx2" ETC="" TermRC="" DESC="" INVCOMMENT="" TermDate="03/01/2014" DestRule="xxxx"> <Asset ID="54321" AXID="edcba" RecvdDt="05/02/2014" ReconDt="05/02/2014" ReceiptID="" /> <Asset ID="09876" AXID="edcba1" RecvdDt="05/02/2014" ReconDt="05/02/2014" ReceiptID="" /> </Contract> <Contract ContractNr="xxx1" ETC="" TermRC="" DESC="" INVCOMMENT="" TermDate="03/01/2014" PO="XXX" > <Asset ID="12345" AXID="abcde" RecvdDt="05/02/2014" ReconDt="05/02/2014" ReceiptID=""/> <Asset ID="67890" AXID="abcde1" RecvdDt="05/02/2014" ReconDt="05/02/2014" ReceiptID=""/> </Contract> <Contract ContractNr="xxx1" ETC="" TermRC="" DESC="" INVCOMMENT="" TermDate="03/01/2014" > <Asset ID="12345" AXID="abcde" RecvdDt="05/02/2014" ReconDt="05/02/2014" ReceiptID=""/> <Asset ID="67890" AXID="abcde1" RecvdDt="05/02/2014" ReconDt="05/02/2014" ReceiptID=""/> </Contract> </R>' IF EXISTS ( SELECT NULL FROM @xml.nodes('/R/Contract') X(Contract) WHERE Contract.value('@PO', 'varchar') IS NULL OR Contract.value('@DestRule', 'varchar') IS NULL ) BEGIN PRINT 'some contracts do not have PO or DestRule' END ELSE BEGIN PRINT 'OK' END