SQL Server返回空字段

时间:2011-03-24 14:58:13

标签: sql sql-server sql-server-2005

我有一个select脚本,它在2005 SQL Server安装中每天运行3次,返回表的几次包含预期的行数但没有任何值。整数和日期字段有零,其他字段只是空白。

由于问题很少发生,因此当脚本运行并且无法复制问题时,无法监督数据库。我的想法是数据库中的其他一些更新导致了问题。有谁知道这个问题?

这是脚本

SELECT DISTINCT
    URL.Line, 
    RTrim(URL.DescriptionNote) AS [DescriptionNote], 
    SA1.Name AS [Name1], 
    SP1.Designation AS [Designation1], 
    SA2.Name [Name2], 
    SP2.Designation AS  [Designation2],
    RL.DistanceMeters,
    dbo.RouteLinkTransportModeDesc(URL.TransportModeTypeNumber) AS TransportMode, 
    URL.THM,
    URL.FirstWorkedDate,
    URL.LastWorkedDate
FROM #RequiredRouteLink URL
    INNER JOIN StopPoint AS SP1
        ON SP1.JourneyPatternPointGid = URL.StartsAtPointGid
    INNER JOIN StopArea AS SA1
        ON SP1.IsPartOfStopAreaVersionId = SA1.VersionId
    INNER JOIN StopPoint AS SP2
        ON SP2.JourneyPatternPointGid = URL.EndsAtPointGid
    INNER JOIN StopArea AS SA2
        ON SP2.IsPartOfStopAreaVersionId = SA2.VersionId
    LEFT JOIN verRouteLink AS RL 
        ON RL.StartsAtPointGid = URL.StartsAtPointGid 
    AND RL.EndsAtPointGid = URL.EndsAtPointGid 
    AND URL.TransportModeTypeNumber = RL.TransportModeTypeNumber 
WHERE URL.StartsAtPointGid <> URL.EndsAtPointGid 
    AND (RL.EndsAtPointGid IS NULL OR RL.DistanceMeters = 0 OR RL.DistanceMeters IS NULL)
    AND RL.[DeletedDateTime] IS NULL
ORDER BY FirstWorkedDate, THM, TransportMode, Line, DescriptionNote

2 个答案:

答案 0 :(得分:1)

您可以运行低级SQL跟踪 - 尽可能地过滤它以减少跟踪的负载[例如,通过应用程序等]

答案 1 :(得分:1)

我确定这是一个数据问题。获得空白字段的原因很可能是LEFT JOIN

你有4 INNER JOIN个,其中任何一个都可能导致你有一个空的结果集。如果其中一个或多个找不到匹配项,则表示没有结果。

,您的LEFT JOIN表示您至少会获得URL.Line的列表,这就是您最有可能获得预期行数的原因。