SQL存储过程不返回任何数据

时间:2010-08-02 13:26:54

标签: sql sql-server

当我在这个表上运行一个简单的SELECT语句时,我得到一行(这是正确的,应该只有一行)。这是SELECT语句:

select * from Lending.HMDA where BatchId = 1

这是我正在执行的proc,它不返回任何行:

    DECLARE @BatchStartDate datetime, @BatchEndDate datetime

    SELECT @BatchStartDate = StartDate, @BatchEndDate = EndDate  FROM Lending.HMDAReportBatch WHERE BatchId = 1

    -- HMDA And App Data
    SELECT
    A.ApplicationId,
    A.CreatedDate,
    LU14.LookupCode AS LoanTypeId,
    LU1.LookupCode AS PropertyTypeId,
    LU2.LookupCode AS LoanPurposeId,
    LU3.LookupCode AS OwnerOccupancyId,
    L.FinalLoanAmount,
    LU4.LookupCode AS PreApprovalId,
    LU5.LookupCode AS ActionId,
    A.ActionDate,
    H.MSA,
    LU6.MiscCode1 AS StateId,
    LU7.LookupCode AS CountyId,
    H.CensusTract,
    LU8.LookupCode AS ApplicantEthnicityId,
    LU9.LookupCode AS JointEthnicityId,
    H.IsApplicantRaceAmericanIndian,
    H.IsApplicantRaceAsian,
    H.IsApplicantRaceBlack,
    H.IsApplicantRaceIslander,
    H.IsApplicantRaceNA,
    H.IsApplicantRaceNotProvided,
    H.IsApplicantRaceWhite,
    H.IsJointRaceAmericanIndian,
    H.IsJointRaceAsian,
    H.IsJointRaceBlack,
    H.IsJointRaceIslander,
    H.IsJointRaceNA,
    H.IsJointRaceNotProvided,
    H.IsJointRaceWhite,
    LU10.LookupCode AS ApplicantGenderId,
    LU11.LookupCode AS JointGenderId,
    LU12.LookupCode AS LoanPurchaserId,
    H.IsDenialReasonCash,
    H.IsDenialReasonCollateral,
    H.IsDenialReasonCreditHistory,
    H.IsDenialReasonDTI,
    H.IsDenialReasonEmploymentHistory,
    H.IsDenialReasonIncomplete,
    H.IsDenialReasonInverifiableInfo,
    H.IsDenialReasonMortgageInsuranceDenied,
    H.IsDenialReasonOther,
    H.RateSpread,
    H.IsHOEPA,
    LU13.LookupCode AS LienStatusId
    --@BatchStartDate AS BatchStartDate,
    --@BatchEndDate AS BatchEndDate
    FROM Lending.HMDA H
    INNER JOIN Lending.Application A ON H.ApplicationId = A.ApplicationId
    INNER JOIN Lending.Loan L ON H.ApplicationId = L.ApplicationId
    INNER JOIN tblLookup AS LU1 ON H.PropertyTypeId = LU1.LookupID
    INNER JOIN tblLookup AS LU2 ON H.LoanPurposeId = LU2.LookupID
    INNER JOIN tblLookup AS LU3 ON H.OwnerOccupancyId = LU3.LookupID
    INNER JOIN tblLookup AS LU4 ON H.PreApprovalId = LU4.LookupID
    INNER JOIN tblLookup AS LU5 ON H.ActionId = LU5.LookupID
    INNER JOIN tblLookup AS LU6 ON H.StateId = LU6.LookupID
    INNER JOIN tblLookup AS LU7 ON H.CountyId = LU7.LookupID
    INNER JOIN tblLookup AS LU8 ON H.ApplicantEthnicityId = LU8.LookupID
    INNER JOIN tblLookup AS LU9 ON H.JointEthnicityId = LU9.LookupID
    INNER JOIN tblLookup AS LU10 ON H.ApplicantGenderId = LU10.LookupID
    INNER JOIN tblLookup AS LU11 ON H.JointGenderId = LU11.LookupID
    INNER JOIN tblLookup AS LU12 ON H.LoanPurchaserId = LU12.LookupID
    INNER JOIN tblLookup AS LU13 ON H.LienStatusId = LU13.LookupID
    INNER JOIN tblLookup AS LU14 ON H.LoanTypeId = LU14.LookupID

    WHERE H.BatchId = 1  AND H.IsExcluded <> 'True'

为什么proc没有返回任何数据?可能出现这种情况的可能情况是什么?

6 个答案:

答案 0 :(得分:6)

如果inner join条件匹配,on仅返回一行。尝试将它们全部更改为left outer join并查看是否有任何行。

left join中,当null条件无法匹配时,表格的列将为on。这通常是查找违规表的快捷方式。

答案 1 :(得分:3)

可能的情况:

  • 没有H.BatchId = 1的行和H.IsExclueded&lt;&gt; '真'
  • 表Application
  • 中没有匹配的行
  • 表Loan中没有匹配的行
  • 对于PropertyTypeId
  • ,tblLookup中没有匹配的行
  • 对于LoanPurposeId
  • ,tblLookup中没有匹配的行
  • 对于OwnderOccupancyId
  • ,tblLookup中没有匹配的行
  • 对于OreApprovalId
  • ,tblLookup中没有匹配的行
  • for ActionUId的tblLookup中没有匹配的行
  • 对于StateId
  • ,tblLookup中没有匹配的行
  • 在CountyId
  • 的tblLookup中没有匹配的行
  • for ApplicantEthnicityId
  • 的tblLookup中没有匹配的行
  • 对于JointEthnicityId
  • ,tblLookup中没有匹配的行
  • 申请人GenderId
  • 的tblLookup中没有匹配的行
  • 对于JointGenderId
  • ,tblLookup中没有匹配的行
  • 对于LoadPurchaserId
  • ,tblLookup中没有匹配的行
  • LienStatusId
  • 的tblLookup中没有匹配的行
  • 对于LoanTypeId
  • ,tblLookup中没有匹配的行

答案 2 :(得分:1)

你是否肯定H.BatchId = 1和H.IsExcluded&lt;&gt; '真正'?如果没有,那就可以做到。

答案 3 :(得分:1)

在不了解您的数据库的情况下,很难说。但我怀疑其中一个Id连接没有返回。如果用子查询替换这些连接,则更容易看出哪一个是罪魁祸首。

例如......

SELECT
    A.ApplicationId,
    A.CreatedDate,
    (SELECT LookupCode
         FROM tblLookup
         WHERE H.LoanTypeId = LookupCode) AS LoantypeId,
   ...etc...

答案 4 :(得分:0)

显然,您的第一个查询和第二个查询是分开的。在第二个查询中,对于所有这些内部联接,您需要Lending.HDMA中的行(并且IsExcluded <> 'True'是第一个查询中不存在的条件)并且具有匹配的行查询中的其他表也是如此。如果您正在寻找过滤掉结果的表,我会评论所有连接并逐个取消注释连接并执行查询以查看是否获得了行。 (显然,您需要暂时将Select子句更改为Select *以执行此操作。)

答案 5 :(得分:0)

查看行消失位置的快速方法可能是在启用“包含实际执行计划”选项的情况下执行查询,并在箭头上快速鼠标以查看行数变为零的位置。