我想从包含xml列(非类型化)的表中选择date,我想验证数据是否符合在数据库中创建的架构集合,并只检索那些符合架构集合的行。表中的列不包含和架构。有人能帮我一下吗!
发布于 2011-09-19 17:45:26
我猜您的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 @XMLTargethttps://stackoverflow.com/questions/7468705
复制相似问题