在下面的查询中,我试图将两个表连接起来:

select
    a1.member_account,
    substring(substring(a1.member_name, charindex('/', a1.member_name)  + 1, len(a1.member_name)), 0, charindex(' ', substring(a1.member_name, charindex('/', a1.member_name) + 1, len(a1.member_name)))) as firstname,
    substring(a1.member_name, 0, charindex('/', a1.member_name)) as lastname,
    a2.account, a2.PriorMonthRewardAmount,
    a2.CurrentMonthRewardAmount, a2.RewardDate, a2.RedemptionAmount,
    a2.RedemptionDate, a1.member_email,
    a1.member_rest_flag11, a1.member_rest_flag12
from
    [NFCUDW].[dbo].[member] as a1
left join
    [IMS].[dbo].[RewardsHistory] as a2 on a2.account = a1.member_account
order by
    DATEPART(year, a2.rewarddate) desc,
    DATEPART(month, a2.rewarddate) desc


这两个表位于同一台服务器中,但位于不同的数据库中,我正在尝试将它们加入。

我得到错误:


消息208,第16级,状态1,第1行
无效的对象名称“ IMS.dbo.RewardsHistory”。


我可以访问两个表,所以我不明白为什么会收到此错误。

首先,我想做的就是加入这两个查询:

select
    *
from
    RewardsHistory
where
    priormonthrewardamount > 0
order by
    DATEPART(year, rewarddate) desc,
    DATEPART(month, rewarddate) desc

select
    member_account,
    substring(substring(member_name, charindex('/', member_name) + 1, len(member_name)), 0, charindex(' ', substring(member_name, charindex('/', member_name) + 1, len(member_name)))) as firstname,
    substring(member_name, 0, charindex('/', member_name)) as lastname,
    member_email, member_rest_flag11, member_rest_flag12
from
    member

最佳答案

我在这里可能是完全错误的,只是黑暗中的一枪而已,但是您在查询中声明别名的部分

as lastname,a2.account, a2.PriorMonthRewardAmount,
   a2.CurrentMonthRewardAmount,a2.RewardDate,a2.RedemptionAmount,a2.RedemptionDate,
   a1.member_email,a1.member_rest_flag11,a1.member_rest_flag12


似乎会引起问题。它看起来确实令人困惑,但是别名不应该是单个术语还是带引号的字符串?您已经包括了整个列表。

10-08 20:00