Bla*_*rel 7 t-sql sql-server sql-server-2005
所以我varbinary(max)在SQL Server 2005中得到了一个专栏,它充满了XML.某些地方的某些记录已截断XML,因此它们无效.
这意味着如果我跑了
SELECT CAST(myVarbinaryColumn as XML) ...
Run Code Online (Sandbox Code Playgroud)
它吹了块.
如何过滤/跳过无效的xml?
当我和varchar做了类似的事情,据说我可以使用日期ISDATE(blah) = 1.所以相当ISVALIDXML()不错.
请不要评论"为什么不是列XML数据类型.."这发生在过去,我没有时间机器.
我认为最好的选择是编写一个自定义CLR 函数,也许使用XmlDocument.Load。在 CLR 中,您可以捕获加载失败时的错误并返回适当的结果。
编辑:下面的代码也可以工作,尽管它不如 UDF 那么优雅。不幸的是,我们不能在 UDF 中使用 TRY/CATCH。
create procedure dbo.usp_IsValidXML(@XMLCandidate varbinary(max), @Return bit output)
as
begin
declare @x xml
begin try
set @x = cast(@XMLCandidate as xml)
set @Return = 1
end try
begin catch
set @Return = 0
end catch
end
go
declare @test1 varbinary(max)
declare @test2 varbinary(max)
set @test1 = cast('<data>asdf</data>' as varbinary(max))
set @test2 = cast('<data>asdf</da' as varbinary(max))
declare @IsValid bit
exec dbo.usp_IsValidXML @test1, @IsValid output
select @IsValid
exec dbo.usp_IsValidXML @test2, @IsValid output
select @IsValid
drop procedure dbo.usp_IsValidXML
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
733 次 |
| 最近记录: |