具有包含 2 条记录的表变量的完整工作示例:
declare @t table (xmldata xml);
insert @t values ('
<AddressBook>
<Address>
<ID>-1</ID>
<AddressID>PRIMARY</AddressID>
<FirstName>JOHN L.</FirstName>
<LastName>JOHSON/JOHN L.</LastName>
<StreetLine1>123 NOWHERE ST</StreetLine1>
<City>OVERHERE</City>
<StateName>CA</StateName>
<StateCode>5</StateCode>
<PostalCode>12345</PostalCode>
<CountryName>United States</CountryName>
<CountryCode>en-US</CountryCode>
<PhoneNumber>5551231234</PhoneNumber>
<PhoneNumber2>5551231234</PhoneNumber2>
<TaxScheduleID>CA</TaxScheduleID>
<ShowAddress>1</ShowAddress>
</Address>
<Address>
<ID>-1</ID>
<AddressID>SECONDARY</AddressID>
<FirstName>SAM</FirstName>
<LastName>LARSON/SAM</LastName>
<StreetLine1>456 OVERHERE</StreetLine1>
<City>NOTTHERE</City>
<StateName>CA</StateName>
<StateCode>5</StateCode>
<PostalCode>54321</PostalCode>
<CountryName>United States</CountryName>
<CountryCode>en-US</CountryCode>
<PhoneNumber>5553334444</PhoneNumber>
<PhoneNumber2>5553334444</PhoneNumber2>
<TaxScheduleID>CA</TaxScheduleID>
<ShowAddress>1</ShowAddress>
</Address>
</AddressBook>');
insert @t values ('
<AddressBook>
<Address>
<ID>-1</ID>
<AddressID>PRIMARY</AddressID>
<FirstName>JANE L.</FirstName>
<LastName>JOHSON/JANE L.</LastName>
<StreetLine1>123 NOWHERE ST</StreetLine1>
<City>OVERHERE</City>
<StateName>CA</StateName>
<StateCode>5</StateCode>
<PostalCode>12345</PostalCode>
<CountryName>United States</CountryName>
<CountryCode>en-US</CountryCode>
<PhoneNumber>5551231234</PhoneNumber>
<PhoneNumber2>5551231234</PhoneNumber2>
<TaxScheduleID>CA</TaxScheduleID>
<ShowAddress>1</ShowAddress>
</Address>
<Address>
<ID>-1</ID>
<AddressID>SECONDARY</AddressID>
<FirstName>NIGEL</FirstName>
<LastName>NIGEL JR./NIGEL</LastName>
<StreetLine1>456 OVERHERE</StreetLine1>
<City>NOTTHERE</City>
<StateName>CA</StateName>
<StateCode>5</StateCode>
<PostalCode>54321</PostalCode>
<CountryName>United States</CountryName>
<CountryCode>en-US</CountryCode>
<PhoneNumber>5553334444</PhoneNumber>
<PhoneNumber2>5553334444</PhoneNumber2>
<TaxScheduleID>CA</TaxScheduleID>
<ShowAddress>1</ShowAddress>
</Address>
</AddressBook>');
update t
set xmldata =
(select n.a.value('ID[1]','nvarchar(max)') ID,
n.a.value('AddressID[1]','nvarchar(max)') AddressID,
n.a.value('FirstName[1]','nvarchar(max)') FirstName,
LEFT(n.a.value('LastName[1]','nvarchar(max)'), LEN(n.a.value('LastName[1]','nvarchar(max)'))
-1-LEN(n.a.value('FirstName[1]','nvarchar(max)'))) LastName,
n.a.value('StreetLine1[1]','nvarchar(max)') StreetLine1,
n.a.value('City[1]','nvarchar(max)') City,
n.a.value('StateName[1]','nvarchar(max)') StateName,
n.a.value('StateCode[1]','nvarchar(max)') StateCode,
n.a.value('PostalCode[1]','nvarchar(max)') PostalCode,
n.a.value('CountryName[1]','nvarchar(max)') CountryName,
n.a.value('CountryCode[1]','nvarchar(max)') CountryCode,
n.a.value('PhoneNumber[1]','nvarchar(max)') PhoneNumber,
n.a.value('PhoneNumber2[1]','nvarchar(max)') PhoneNumber2,
n.a.value('TaxScheduleID[1]','nvarchar(max)') TaxScheduleID,
n.a.value('ShowAddress[1]','nvarchar(max)') ShowAddress
from AddressBook.nodes('Address') n(a)
for xml path('Address'), root('AddressBook'), type
)
from @t t
cross apply xmldata.nodes('AddressBook') ab(AddressBook);
select *
from @t;