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 41 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. AndreiFomitchev 16 Reputation points
    2021-04-12T21:04:12.433+00:00

    Working Example: CREATE DATABASE testDB1; CREATE DATABASE testDB2; USE testDB1 GO CREATE TABLE TestTable1(id1 Int) GO USE testDB2 GO CREATE TABLE TestTable2(id2 Int); GO INSERT INTO testDB1.dbo.TestTable1(id1) VALUES (1234);

    INSERT INTO testDB2.dbo.TestTable2(id2) SELECT id1 FROM testDB1.dbo.TestTable1;

    SELECT * FROM testDB2.dbo.TestTable2 -- It returns 1234.

    1. Use <DB Name>.<Schema Name>.<Table Name>
    2. Check that your account has access for SELECT / INSERT in the databases/tables.

    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.