SQL-异常行为,查询会抛出NullReferenceException,除非使用了无关参数

时间:2019-02-25 23:00:32

标签: sql mysql-spatial

这是一些非常奇怪的行为。我有一个SQL查询抛出NullReferenceException。查询是:

SELECT
    rtrim(h.fieldA) AS fieldA,
    h.fieldB AS fieldB,
    dbo.GeographyUnionAggregate(dbo.MakeValidGeographyFromGeometry(l.geom).STIntersection(dbo.MakeValidGeographyFromGeometry(h.geom))).STArea() AS Area
FROM dbo.tableL AS l
    INNER JOIN dbo.tableH AS h
        ON l.geom.STIntersects(h.geom) = 1
WHERE
    l.myField = 568
GROUP BY h.fieldA, h.fieldB
ORDER BY h.fieldB, Area DESC

这将引发此错误:

> Msg 6522, Level 16, State 1, Line 1
A .NET Framework error occurred during execution of user-defined routine or aggregate "GeographyUnionAggregate":
System.NullReferenceException: Object reference not set to an instance of an object.
System.NullReferenceException:
   at SQLSpatialTools.GeographyCollectionAggregate.Merge(GeographyCollectionAggregate group)

但是!如果我们在WHERE子句中添加一个额外条件:

SELECT
    rtrim(h.fieldA) AS fieldA,
    h.fieldB AS fieldB,
    dbo.GeographyUnionAggregate(dbo.MakeValidGeographyFromGeometry(l.geom).STIntersection(dbo.MakeValidGeographyFromGeometry(h.geom))).STArea() AS Area
FROM dbo.tableL AS l
    INNER JOIN dbo.tableH AS h
        ON l.geom.STIntersects(h.geom) = 1
WHERE
    l.myField = 568
    AND l.AnotherField = 22
GROUP BY h.fieldA, h.fieldB
ORDER BY h.fieldB, Area DESC

它将正常工作。

现在,这是奇数位。因为l.myField = 568的结果是两个记录,而l.AnotherField = 22的结果是完全相同的两个记录。因此,它在没有第二个参数的情况下应该与第一个参数一样运行-数据没有任何区别。我唯一能想到的是,根据索引获取数据的时间可能有所不同,但是两个字段都已建立索引,并且在两种情况下获取数据完全不需要时间。

有人有什么想法,要检查的东西吗?此外,这在我们的开发服务器上也能正常工作,该服务器当前使用相同的数据和相同的GeographyUnionAggregate函数。

0 个答案:

没有答案