Share via

Insert table and prevent duplicates syntax

Debilon 431 Reputation points
2022-04-20T16:04:48.97+00:00

I have two tables in two different databases holding part of the same information

BigPhoneDB SmallPhoneDB and BigPhoneTBL and SmallPhoneTBL

i am trying to merge the data from SmallPhoneDB.SmallPhoneTBL into BigPhoneDB.BigPhoneTBL while preventing duplicate entries of course

use BigPhoneDB;

Insert into [BigPhoneDB].[BigPhoneTble]( [FirstName],[LastName],[Address],[City],[State],[Zip],[PhoneNumber])

Select [FirstName],[LastName],[Address],[City],[State],[Zip],[PhoneNumber]
From [SmallPhoneDB].[SmallPhoneTBL]

where [SmallPhoneDB].[SmallPhoneTBL] not in ([BigPhoneDB].[BigPhoneTble].[PhoneNumber] )

And I'm getting an error msg that The multi-part identifier "[BigPhoneDB].[BigPhoneTble].[PhoneNumber]" could not be bound.
and i don't understand why.

Any Idea is welcome

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.

0 comments No comments

Answer accepted by question author

Naomi Nosonovsky 8,906 Reputation points
2022-04-20T16:22:26.617+00:00

You may be better using MERGE command syntax to do the same operation.

Was this answer helpful?

0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Bert Zhou-msft 3,521 Reputation points
    2022-04-21T02:51:05.773+00:00

    Hi,@Debilon

    Welcome to Microsoft T-SQL Q&A Forum!

    The reason for the failure to bind is: the where filter reference column does not find any location definition, which means that there is a problem with your syntax.
    Look carefully, you want to implement data transfer across databases, but the two database schemas are inconsistent. It needs to be specified separately.
    Erland gives the way to add the schema.
    194856-image.png
    Secondly, your where syntax is wrong. We usually say that PhoneNumber exists in a certain table in a certain database, but not whether the table in that database contains this field.
    Please refer to this document.

    Best regards,
    Bert Zhou


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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?

    0 comments No comments

  2. Erland Sommarskog 134.7K Reputation points MVP Volunteer Moderator
    2022-04-20T21:51:17.62+00:00

    Three bright minds answering - and all missing the mistake. How is that possible?

    When you make a cross-database reference, you need three components: 1) Database 2) Schema. 3) Table. You are missing the schema. (Whereupon SQL Server interprets the database name as the schema name, and it goes downhill from there.)

    Thus, you need:

    Insert into [BigPhoneDB].[BigPhoneTble]( [FirstName],[LastName],[Address],[City],[State],[Zip],[PhoneNumber])
    

    should be:

    Insert into BigPhoneDB.dbo.BigPhoneTble(FirstName, LastName, Address, City, State, Zip, PhoneNumber)
    

    (And, yeah, I recommend not having brackets all over the place, to prevent you from wearing down your fingers by typing them and your eyes by looking them.)

    dbo is the default schema for most users, and it is also possible to say

    Insert into BigPhoneDB..BigPhoneTble(FirstName, LastName, Address, City, State, Zip, PhoneNumber)
    

    and SQL Server will fill in dbo in the missing space.

    The same applies of course to the rest of the cross-db references in the script as well.

    Was this answer helpful?


  3. Tom Phillips 17,786 Reputation points
    2022-04-20T17:39:51.557+00:00

    Just to be clear.

    You cannot use the target table in your SELECT statement. The SELECT statement is standalone from the INSERT portion.

    Was this answer helpful?

    0 comments No comments

  4. Tom Cooper 8,501 Reputation points
    2022-04-20T16:18:21.167+00:00
    Insert into [BigPhoneDB].[BigPhoneTble]( [FirstName],[LastName],[Address],[City],[State],[Zip],[PhoneNumber])
    
    Select [FirstName],[LastName],[Address],[City],[State],[Zip],[PhoneNumber]
    From [SmallPhoneDB].[SmallPhoneTBL] sp
    
    where sp.[PhoneNumber] not in (Select bp.[PhoneNumber] From [BigPhoneDB].[BigPhoneTble] bp )
    

    Tom

    Was this answer helpful?

    0 comments No comments

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.