对于以下查询,数据无法正确提取

时间:2015-11-09 21:44:45

标签: sql-server-2012

Update Tbl_PPACA_Import set
    LASTNAME = LTRIM(RTRIM(A.LASTNAME)),
    FIRSTNAME = LTRIM(RTRIM(A.FIRSTNAME)),
    LEVEL1CODE = LTRIM(RTRIM(ISNULL(A.LEVEL1CODE,ISNULL((
                    SELECT TOP 1 Location_Code
                    FROM Tbl_Location 
                    WHERE Employer_Id=@Employer_Id 
                    AND Location like '%'+LTRIM(RTRIM(A.LEVEL1DESCRIPTION))+'%' ),
    LTRIM(RTRIM(A.LEVEL1DESCRIPTION)))))),
    LEVEL1DESCRIPTION = LTRIM(RTRIM(isnull(A.LEVEL1DESCRIPTION,A.LEVEL1CODE))),
    LEVEL2CODE=LTRIM(RTRIM(A.LEVEL2CODE)),
    LEVEL2DESCRIPTION=LTRIM(RTRIM(A.LEVEL2DESCRIPTION)),
    LEVEL3CODE=LTRIM(RTRIM(A.LEVEL3CODE)),
    LEVEL3DESCRIPTION=LTRIM(RTRIM(A.LEVEL3DESCRIPTION)),
    LEVEL4CODE=LTRIM(RTRIM(A.LEVEL4CODE)),
    LEVEL4DESCRIPTION=LTRIM(RTRIM(A.LEVEL4DESCRIPTION)),
    LEVEL5CODE=LTRIM(RTRIM(A.LEVEL5CODE)),
    LEVEL5DESCRIPTION=LTRIM(RTRIM(A.LEVEL5DESCRIPTION)),
    UNIONCODE=LTRIM(RTRIM(A.UNIONCODE)),
    SSN=LTRIM(RTRIM(REPLACE(A.SSN,'-',''))),
    -- The following line is the line in question
    DATEOFHIRE=(select top 1  ISNULL(DOHIRE,cast('01-01-1900' as date))
                    from tbl_basicinfo
                    where ssn=a.ssn
                    and employer_Id=a.employeriD)       
    from Tbl_PPACA_Import A

注意::此查询的最后一行不会从tbl_basicinfo表中提取DOHIRE列值..此查询的其他部分工作正常,但最后一行不会提取DOHIRE。

1 个答案:

答案 0 :(得分:1)

我只是选择了似乎无法正常工作的部分:

 DATEOFHIRE=(select top 1  ISNULL(DOHIRE,cast('01-01-1900' as date))
                    from tbl_basicinfo
                    where ssn=a.ssn
                    and employer_Id=a.employeriD)  

当您使用ISNULL时,无论如何都应该有结果 - 除非根本没有数据!

请尝试以下操作:注释WHERE部分(双减去,请注意最后一个括号!)

 DATEOFHIRE=(select top 1  ISNULL(DOHIRE,cast('01-01-1900' as date))
                    from tbl_basicinfo
                    --where ssn=a.ssn
                    --and employer_Id=a.employeriD
             )  

您将无法获得正确的日期,但会有一个日期。我是对的吗?

如果是,我认为,没有适合ssn=a.ssn and employer_Id=a.employeriD

的数据