Wrox Programmer Forums
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 6th, 2006, 12:24 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
Default insert into

I would like to insert into table1 all the unique MFG,MAKE,MODEL fields from the union of 3 other tables.

table1 has MFG,MAKE,MODEL set as the primary key.

So I use INSERT INTO and UNION statements to store the values. This would work fine without primary key, but is there any way to specify ignoring PRIMARY KEY constraint errors while running a query?

 
Old June 6th, 2006, 01:03 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

If you are getting primary key constraint errors, it's because you are trying to insert rows with a duplicate key. While you could temporarily remove the columns as the primary key, you'd get the errors right back as soon as you tried to reestablish them as the key, with the added problem that now you've got multiple rows in the table with the same MFG,MAKE,MODEL combination.

By definition, the primary key must be unique.

Your INSERT query is generating duplicate values for some MFG,MAKE,MODEL combinations.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old June 6th, 2006, 03:55 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am aware of the reason for the error...

What I would like to end up with is a table with all unique MFG, MAKE, MODEL combinations.

This can be done in ACCESS by appending to a empty table and ignoring the primary key violations. New records that violate the constraint are not appended...

What I'm trying to do is the same thing in SQL Server by creating a SP that will result in the same thing.

 
Old June 6th, 2006, 06:35 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

I must be missing something here, but if you are aware of the reason for the error, and you want a table with unique MFG, MAKE, MODEL combinations, then, have your query select unique values for your INSERT.

I suppose you could instead insert rows one at a time from within a cursor and ignore the constraint errors. You could also do the INSERT with no primary key defined, then remove the duplicates after the INSERT (this can be tricky, though), and then establish the primary key. But I know of no set-based way to do what you want.

I've heard it said from Relational purists that it can take over a year to recover from Access poisoning. :D

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to insert " ". Somesh XML 1 March 6th, 2007 01:30 PM
How to insert " ". Somesh C# 1 March 6th, 2007 08:45 AM
trigger to insert current date on insert kev_79 SQL Server 2000 3 January 23rd, 2006 05:58 PM
Insert into jemacc SQL Server 2000 2 March 31st, 2004 08:31 AM
"INSERT INTO" kaz SQL Language 7 December 15th, 2003 07:40 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.