Question

I am looking to compare 2 tables from 2 databases on the same server and insert that data into a 3rd table. I thought this would be easy but am not sure how to do this with 2 databases. The code below is not liked by SQL Server. Any help would be great.

INSERT INTO WaterAccounts (CaseNumberKey, MASTER_ACCOUNT, 
                            OWNER_NAME, MAIL_NAME, ACCT_BALANCE)
   SELECT 
      B.CaseNumberKey, 
      C.MASTER_ACCOUNT, 
      C.OWNER_NAME, 
      C.MAIL_NAME, 
      C.ACCT_BALANCE
   FROM 
      newCityCollection.PropertyInformation B, DEM C
   WHERE 
      B.Name = C.SERV_STREET 
      AND B.DIRECTION = C.SERV_DIRECTION 
      AND B.NUM = C.SERV_STREET_NO
Était-ce utile?

La solution

If the newCityCollection is the other database, and PropertyInformation the table in it, you're missing the schema name. Assuming it's dbo (probably is), you shoud write something like this:

   INSERT INTO WaterAccounts (CaseNumberKey, MASTER_ACCOUNT, OWNER_NAME, MAIL_NAME, ACCT_BALANCE)
   SELECT 
      B.CaseNumberKey, C.MASTER_ACCOUNT, C.OWNER_NAME, 
      C.MAIL_NAME, C.ACCT_BALANCE
   FROM newCityCollection.dbo.PropertyInformation B
   INNER JOIN DEM C
      ON B.Name = C.SERV_STREET 
        AND B.DIRECTION = C.SERV_DIRECTION 
        AND B.NUM = C.SERV_STREET_NO
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top