need help regarding querying xml in sql server
i have two table in sql server 2005. in one table table1 there is one column whose data type is xml where we save data in x开发者_运维问答ml format. now i have another table table2 where we store few fileds name. so now i want to write query in such a on xml data which will return those fields value which are defined in table2. how to achieve it with simple sql statement instead of store procedure.
my xml structure and data like below one
<Record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<DELETED>
<JID>41185</JID>
<WID>0</WID>
<AccountReference>LH169</AccountReference>
<OEReference>Ari002</OEReference>
<InvoiceNumber>0</InvoiceNumber>
<OrderPlacedBy>Mark Catterall</OrderPlacedBy>
<Specialist>0FFICINA MOTTAUTO</Specialist>
<Priority>2</Priority>
<JobType>OUR</JobType>
<JobState>NOTSTARTED</JobState>
<JobAddedDate>2011-05-31T16:17:00</JobAddedDate>
<JobStartedDate>2011-05-31T16:18:00</JobStartedDate>
<JobFinishedDate>1777-01-01T00:00:01</JobFinishedDate>
<JobShippedDate>1777-01-01T00:00:01</JobShippedDate>
<RecievedDate>1777-01-01T00:00:01</RecievedDate>
<UPSShippingNumber />
<CustomerName>02 IRELAND</CustomerName>
<ContactName>ALAN CONLAN</ContactName>
<Telephone>00353868377926</Telephone>
<StandardHours>3.00</StandardHours>
<JobDescription>test for search 2</JobDescription>
<UserName xsi:nil="true" />
<AwaitingCustomer>0</AwaitingCustomer>
<ReturnToCore>0</ReturnToCore>
<AwaitingFromSalvage>0</AwaitingFromSalvage>
<PartDescription>test for search 2</PartDescription>
<PostalCode>IRELAND</PostalCode>
<OURPrice xsi:nil="true" />
<ExchangePrice xsi:nil="true" />
<NewPrice xsi:nil="true" />
<eBayPrice xsi:nil="true" />
<Status>UPDATED</Status>
</DELETED>
</Record>
suppose in my table2 few fields are stored like JID,WID,AccountReference,OEReference,InvoiceNumber.
so please guide me how to write sql on xml data which will return only JID,WID,AccountReference,OEReference,InvoiceNumber from xml data but filed name will not be hard coded rather it will be fetch from another table table2.
please guide me.
To read data from xml you can use like this:
Select
MyXmlColumn.value('(Record/DELETED/JID)[1]', 'int' ) as JID,
MyXmlColumn.value('(Record/DELETED/WID)[1]', 'int' ) as WID,
MyXmlColumn.value('(Record/DELETED/AccountReference)[1]', 'nvarchar(255)' ) as AccountReference from table2
[update]
create a stored procedure with your parameters:
create procedure getmyxmldata
(
@param1 varchar(50),
@param2 varchar(50)
)
as
begin
declare @myQuery varchar(1000);
set @myQuery = 'Select
MyXmlColumn.value(''(Record/DELETED/' + @param1 + ')[1]'', ''nvarchar(255)'' ) as ' + @param1 + ',
MyXmlColumn.value(''(Record/DELETED/' + @param1 + ')[1]'', ''nvarchar(255)'' ) as ' + @param1 + ' from table2';
EXEC sp_executesql @myQuery
end
It is also possible to read the specified fields from the other table in this stored procedure and create your select statement dynamically without passing parameters.
[Update 2]
you can try this for multiple deleted tags:
Select
Row.value('(./JID)[1]', 'int' ) as JID,
Row.value('(./WID)[1]', 'int' ) as WID,
Row.value('(./AccountReference)[1]', 'nvarchar(255)' ) as AccountReference
from table2
CROSS APPLY MyXmlColumn.nodes('/Record/DELETED') as Record(Row)
精彩评论