0

在这方面需要帮助。

我只是想知道为什么会发生这个错误。下面是相关的脚本。

    SELECT loc.ID
    ,loc.LocCode
    ,loc.LocName
    ,st.StateName
    ,reg.RegionName
    ,ctry.CountryName
    ,ISNULL(CONVERT(DATE, loc.UpdatedDate), CONVERT(DATE,loc.CreatedDate)) AS [ModifiedDate]
    ,stf.Name AS [ModifiedBy]
FROM Spkr_Country AS ctry WITH (NOLOCK)
INNER JOIN Spkr_Location AS loc WITH (NOLOCK) ON ctry.ID = loc.CountryID
INNER JOIN Spkr_State AS st WITH (NOLOCK) ON loc.StateID = st.ID
INNER JOIN Spkr_Region AS reg WITH (NOLOCK) ON loc.RegionID = reg.ID
INNER JOIN Staff AS stf ON ISNULL(loc.UpdatedBy, loc.CreatedBy) = stf.StaffId
WHERE (loc.IsActive = 1)
    AND (
        (@LocCode = '')
        OR (
            @LocCode <> ''
            AND loc.LocCode LIKE @LocCode + '%'
            )
        )
    AND (
        (@RegionID < 1)
        OR (
            @RegionID > 0
            AND loc.RegionID = @RegionID
            )
        )
    AND (
        (@StateID < 1)
        OR (
            @StateID > 0
            AND loc.StateID = @StateID
            )
        )
    AND (
        (@CountryID < 1)
        OR (
            @CountryID > 0
            AND loc.CountryID = @CountryID
            )
        )

错误可能发生在这里INNER JOIN Staff AS stf ON ISNULL(loc.UpdatedBy, loc.CreatedBy) = stf.StaffId我想要的要求是如果loc.UpdatedBy为null,它将使用该loc.CreatedBy列。但是,当我使用它时,它会产生提到的错误。

在数据库中,loc.CreatedBy不为空,而 可以loc.UpdatedBy为空。

我通过运行脚本检查了它,但它工作正常。

我该怎么办?我的代码有什么问题?请帮忙。

4

1 回答 1

0

我将内部连接条件更改为这行代码。 INNER JOIN Staff AS stf ON COALESCE(loc.UpdatedBy, loc.Createdby) = stf.StaffId

但是,我仍然不知道为什么它不起作用?在这种情况下使用COALESCE()and有什么区别?ISNULL()

于 2012-06-19T09:13:55.823 回答