Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SELECT INTO Query Help

Author  Topic 

cnbhold
Starting Member

43 Posts

Posted - 2010-08-04 : 19:28:18
I have a table called Invoice and the only primary key is a field called InvoiceID (autonumber). This table has records with duplicate invoice numbers and part numbers. I need a way to add additional records to this table since the first batch of data is missing some records. I have a another table that contains the orignal data plus the missing records. Getting ride of the orignal data is not an option. I need to only inport records where the Invoice number is not already listed in the Invoice table. Need help with the Where statement that will some how not inport a record if the Invoice number already exist in the table.

Angel

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-04 : 19:32:37
--INSERT INTO Invoice (...)
SELECT * FROM OtherTable o
WHERE NOT EXISTS (SELECT * FROM Invoice i WHERE i.InvoiceNo = o.InvoiceNo)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

cnbhold
Starting Member

43 Posts

Posted - 2010-08-04 : 21:18:02
Thanks Tara, that worked!

Angel
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2010-08-04 : 22:48:25
>> I have a table called Invoice .. <<

This name says you have only one of them. Please read any book on data modeling.

>> .. and the only [do you usual have more than one as you imply?] PRIMARY KEY is a field [columns are nothing fields] called invoice_id [do you know the difference between an identifier and a tag number, such as an Invoice Number? Obviously not] (autonumber) [bad design – we want check digits and other properties] <<

So far, your DDL is a mess ..

>> This table has records [sic] with duplicate invoice numbers and part numbers. <<

What is the key? Since there are dups, it cannot be invoice numbers.

Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. If you know how, follow ISO-11179 data element naming conventions and formatting rules. Temporal data should use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect.

Sample data is also a good idea, along with clear specifications. It is very hard to debug code when you do not let us see it. If you want to learn how to ask a question on a Newsgroup, look at: http://www.catb.org/~esr/faqs/smart-questions.html

>> I need to only import records [sic] where the Invoice number is not already listed in the Invoices table.<<

It might be a job for a MERGE statement, but who knows without any DDL or specifications?

--CELKO--
Joe Celko, SQL Guru
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-05 : 11:07:52
quote:
Originally posted by cnbhold

Thanks Tara, that worked!



You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -