Invalid Object Name when attempting to insert data from a table in one DB to a table in another DB on the same server

James Hackney 61 Reputation points
2021-04-12T19:38:47.667+00:00

I am attempting to perform an insert from a table in one database to a table in another database on the same server I have tried this:

INSERT INTO SDCS_AUDIT.BMA_FORM_DATA (.....)

and this:

INSERT INTO [SDCS_AUDIT].[BMA_FORM_DATA] (...)

as well as this:

INSERT INTO DATABASE_Name.SDCS_AUDIT.BMA_FORM_DATA (...)

and this:

INSERT INTO [DATABASE_Name].[SDCS_AUDIT].[BMA_FORM_DATA] (...)

I have also attempted to add the server name in front of the database name but it doesn't like that either.

I know that you need to use sp_linkedserver to create a linked server if the dbs are on two separate servers but from my understanding I shouldn't have to do that if they are on the same server.

Intellisense doesn't show any of the other databases on this server. Is there an option I have to turn on/off somewhere or am I making a bonehead mistake somewhere? This is inside of a trigger by the way, in case that matters.

Azure SQL Database
Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.


6 answers

Sort by: Newest
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more

  3. MelissaMa-msft 24,246 Reputation points Moderator
    2021-04-14T02:12:18.333+00:00

    HI @James Hackney ,

    Thanks for your update.

    Actually I am not familar with Azure SQL Database.

    I did some research and found one useful article which introduces some detailed steps of cross-database query in Azure SQL Database.

    After creating two tables in different Azure SQL database, you also need to create database scoped master key and credentials ,External data sources and External tables.

    Please refer more details from below and check whether it is helpful:
    Cross-database Query in Azure SQL Database

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    1 person found this answer helpful.

  4. Erland Sommarskog 138.7K Reputation points MVP Volunteer Moderator
    2021-04-13T21:21:20.14+00:00

    Also, this is Azure SQL Database instead of SQL Server if that makes a difference.

    Yes, that makes a difference. You can't make cross-database references in Azure SQL DB. In Managed Instance you can, but not DB.

    Was this answer helpful?


  5. MelissaMa-msft 24,246 Reputation points Moderator
    2021-04-13T03:21:44.807+00:00

    Hi @James Hackney ,

    Welcome to Microsoft Q&A!

    You could refer Andrei's example and check whether any is different or missing.

    Besides, it could be recommended to post the DDL and sample data together with the error message.

    You could confirm existance of a table using following query:

    USE DATABASE_Name  
    SELECT OBJECT_ID('SDCS_AUDIT.BMA_FORM_DATA','U')  
    

    OR

    SELECT OBJECT_ID('DATABASE_Name.SDCS_AUDIT.BMA_FORM_DATA','U')  
    

    If output is not NULL then your table already exists.

    In addition,regarding the errror message 'invalid object name', you can make sure below points:

    • Check you are connectiong to the correct database.
    • In SSMS,go to Edit -> IntelliSense ->Refresh Local Cache or right click the database, and click "refresh".
    • Check you have all permission on that table including view definition.

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.