Я предполагаю, что у вас есть XML в столбце XML, который не связан с коллекцией схем, и вы хотите извлечь строки, которые соответствуют этой коллекции.
Вы можете сделать это в цикле, перемещая по одной строке за раз, ловя ошибки из вставки, когда XML не подходит.
XMLSchema:
create xml schema collection ItemList as '
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="item" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>'
Пример кода, перемещающего XML из @XMLSource в @XMLTarget:
-- Setup the source table
declare @XMLSource table (ID int identity, XMLCol xml)
insert into @XMLSource values ('<root><item>X</item></root>')
insert into @XMLSource values ('<root>Invalid row</root>')
insert into @XMLSource values ('<root><item>Y</item></root>')
-- Target table with XMLCol using schema ItemList
declare @XMLTarget table(XMLCol xml(ItemList))
declare @ID int
select @ID = min(ID)
from @XMLSource
-- Move one row at a time
while @ID is not null
begin
begin try
insert into @XMLTarget (XMLCol)
select XMLCol
from @XMLSource
where ID = @ID
end try
begin catch
end catch
select @ID = min(ID)
from @XMLSource
where ID > @ID
end
select *
from @XMLTarget