Solved

Insert into a different database using where clause on a column

Posted on 2010-08-16
5
447 Views
Last Modified: 2012-08-14
I am having trouble with the last item..the where clause in my insert statement.
I keep getting an error of.. "The multi-part  identifier ... could not be bound."
Any ideas greatly appreciated!

INSERT INTO [targetdatabase].[dbo].[mytable]( field1, field2,field3)
SELECT field1, field2, field3
FROM [sourcedatabase].[dbo].[mytable]
where [targetdatabase].[dbo].[mytable].myfield not in(select myfield from sourcedatabase)
0
Comment
Question by:H-SC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 7

Expert Comment

by:lundnak
ID: 33447287
This error is generally a problem with the syntax of your select statement.
Do you have table aliases defined in the FROM clause?  I double check the table aliases against the fields in the SELECT and WHERE clauses.
0
 
LVL 7

Expert Comment

by:lundnak
ID: 33447321
For Example:

Select tbl1.fld1, tbl2.fld1
from table1 as tbl1
join table2 as tbl2 on tbl2.id = tbl1.id

If the fields in the SELECT clause do not have a unique name and the table alias assignment is missing, then you will get this error.
You will also get the error if you misspell a table alias.  (i.e. tbbl2.fld1 instead of tbl2.fld1)

Make sure you check the GROUP BY and ORDER BY clauses also.
0
 
LVL 1

Author Comment

by:H-SC
ID: 33447340
lundnak,

I am not using aliases
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 33447516
please try
INSERT INTO [targetdatabase].[dbo].[mytable]( field1, field2,field3)
SELECT field1, field2, field3
FROM [sourcedatabase].[dbo].[mytable] t
where not exists ( select null from sourcedatabase s where t.myfield = s.myfield )

Open in new window

0
 
LVL 1

Author Comment

by:H-SC
ID: 33448280
angelIII,

Perfect!

many thank you's
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question