Why is the error "invalid object name" if the tables refer to the same server in different databases?

In the following example, I am trying to join both tables:

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

      

These two tables are on the same server but different databases and I am trying to join them.

I get an error:

Msg 208, Level 16, State 1, Line 1
Invalid object name 'IMS.dbo.RewardsHistory'.

I have access to both tables, so I don't understand why I am getting this error.

I first tried to combine these two requests:

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

      

+3


source to share


1 answer


I could be completely wrong, just a shot in the dark, but the part of your request where you declare an alias

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

      



it looks like it will cause problems. This certainly looks confusing, but not an alias that should be a single term or quote? You've included a whole list.

-1


source







All Articles