I'm having a little problem with using Select Not Exists between 2 tables.
I get the infamous Primary key "Cannot insert duplicate key" error.
This is on MS Sql 2000.
I'm trying to create a temp table with distinct values from another table.
Heres a small snippet of the code.
Create table ##tblMail
(ClientID Integer)
Insert into ##tblMail (ClientID) Values (1)
Insert into ##tblMail (ClientID) Values (1)
Insert into ##tblMail (ClientID) Values (2)
Insert into ##tblMail (ClientID) Values (2)
Drop Table ##TmpMail
GO
Create Table ##TmpMail
(ClientID Integer
Primary Key (ClientID))
GO
Insert into ##TmpMail
(ClientID)
select Mail.ClientID
from ##tblMail Mail
where NOT EXISTS (SELECT DISTINCT(##TmpMail.ClientID)
FROM ##TMPMAIL
WHERE Mail.ClientID = ##TmpMail.ClientID)
Thanks
Randy http://members.aol.com/rsmeiner 4 5361
"RSMEINER" <rs******@aol.comcrap> wrote in message
news:20***************************@mb-m25.aol.com... I'm having a little problem with using Select Not Exists between 2 tables. I get the infamous Primary key "Cannot insert duplicate key" error. This is on MS Sql 2000.
I'm trying to create a temp table with distinct values from another table.
Heres a small snippet of the code.
Create table ##tblMail (ClientID Integer) Insert into ##tblMail (ClientID) Values (1) Insert into ##tblMail (ClientID) Values (1) Insert into ##tblMail (ClientID) Values (2) Insert into ##tblMail (ClientID) Values (2)
Drop Table ##TmpMail GO Create Table ##TmpMail (ClientID Integer Primary Key (ClientID)) GO
Insert into ##TmpMail (ClientID) select Mail.ClientID from ##tblMail Mail where NOT EXISTS (SELECT DISTINCT(##TmpMail.ClientID) FROM ##TMPMAIL WHERE Mail.ClientID = ##TmpMail.ClientID)
Thanks
Randy http://members.aol.com/rsmeiner
Insert into ##TmpMail
(ClientID)
select distinct ClientID
from ##tblMail m
where NOT EXISTS (SELECT *
FROM ##TMPMAIL t
WHERE m.ClientID = t.ClientID)
Simon
>Insert into ##TmpMail (ClientID) select distinct ClientID from ##tblMail m where NOT EXISTS (SELECT * FROM ##TMPMAIL t WHERE m.ClientID = t.ClientID)
Simon
I got pretty excited there for a second.
Remember I said it was just a snippet ?
Heres the rest
Insert into ##TmpMail
(ClientID,
ScheduleID,
MSType,
ScheduleType,
CarrierCode,
Address1,
Address2,
Address3,
Address4,
Address5,
Address6,
JunkMailCode)
select Mail.ClientID,
Mail.ScheduleID,
MSType,
ScheduleType,
CarrierCode,
Address1,
Address2,
Address3,
Address4,
Address5,
Address6,
JunkMailCode
from ##tblMail Mail
Join tblMailSetup MS
on Mail.ClientID = MS.ClientID
where ((NOT EXISTS (SELECT DISTINCT(##TmpMail.ClientID)
FROM ##TMPMAIL
WHERE Mail.ClientID = ##TmpMail.ClientID))
Randy http://members.aol.com/rsmeiner
The solution is the same.
The NOT EXISTS subquery makes sure that you do not insert rows that
already exist (you don't need the DISTINCT keyword here). In the main
SELECT you need to make sure that you do not add new rows with the same
ID more than once.
If all columns have the same values for the duplicate rows, then you can
still use DISTINCT in your main query (INSERT INTO ... SELECT DISTINCT
.... FROM ...), as suggested by Simon in the previous post.
If not, then the easiest way is to clean the data first before you do
the insert.
HTH,
Gert-Jan
RSMEINER wrote: Insert into ##TmpMail (ClientID) select distinct ClientID from ##tblMail m where NOT EXISTS (SELECT * FROM ##TMPMAIL t WHERE m.ClientID = t.ClientID)
Simon
I got pretty excited there for a second. Remember I said it was just a snippet ?
Heres the rest
Insert into ##TmpMail (ClientID, ScheduleID, MSType, ScheduleType, CarrierCode, Address1, Address2, Address3, Address4, Address5, Address6, JunkMailCode) select Mail.ClientID, Mail.ScheduleID, MSType, ScheduleType, CarrierCode, Address1, Address2, Address3, Address4, Address5, Address6, JunkMailCode from ##tblMail Mail Join tblMailSetup MS on Mail.ClientID = MS.ClientID where ((NOT EXISTS (SELECT DISTINCT(##TmpMail.ClientID) FROM ##TMPMAIL WHERE Mail.ClientID = ##TmpMail.ClientID))
Randy http://members.aol.com/rsmeiner
--
(Please reply only to the newsgroup)
>The solution is the same. The NOT EXISTS subquery makes sure that you do not insert rows that already exist (you don't need the DISTINCT keyword here). In the main SELECT you need to make sure that you do not add new rows with the same ID more than once.
If all columns have the same values for the duplicate rows, then you can still use DISTINCT in your main query (INSERT INTO ... SELECT DISTINCT ... FROM ...), as suggested by Simon in the previous post.
If not, then the easiest way is to clean the data first before you do the insert.
HTH, Gert-Jan
Thanks. I will fool with it later today.
Clean data ? Is there such a thing anymore ? This data is
dirty. Plain and simple. I'm redoing a real ugly and old system.
Randy http://members.aol.com/rsmeiner This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: THC |
last post by:
Is it possible to set READ UNCOMMITTED to a user connecting to an SQL
2000 server instance? I understand this can be done via a front end
application. But what I am looking to do is to assign...
|
by: Sean C. |
last post by:
Helpful folks,
I am having a hard time figuring out how to reduce my percentage of
dirty page steal activity. Below are statistics for three fairly
normal days, with the bufferpool hit ratios...
|
by: Rey |
last post by:
Howdy all.
Appreciate your help with several problems I'm having:
I'm trying to determine if the Visit subform (subformVisits) has a new
record or been changed, i.e. dirty. The form that...
|
by: Salad |
last post by:
A97.
I have a command button to save and exit. I had the code
If Me.Dirty Then
Docmd.RunCommand acCmdSaveRecord
...more code
endif
I was getting an error because a value was not getting...
|
by: RIP662 |
last post by:
Okay all you GURUS out there... here's the skinny... I need a way to
prevent the Me.Dirty event to trigger after a FORM_OPEN event. What I
have is a field that is dependent on another field that...
|
by: Susan Bricker |
last post by:
I am currently using the OnDirty event of a Form to detect whether any
fields have been modified. I set a boolean variable. Then, if the
Close button is clicked before the Save button, I can put...
|
by: Michael Maes |
last post by:
Hi,
I have a BaseClass from which many Classes Derive.
In short: the BaseClass provides the functionalities (Methods) and the Derived Classes extend it with Properties.
One of the (Base)...
|
by: lesperancer |
last post by:
I've got a form that is opened in readonly mode and no fields can be
changed, great
but if I click on a combo box that has an _enter() event that sets a
field on my form to a value (albeit the...
|
by: Nmx |
last post by:
Hi everyone,
I'm writing a patch to a search engine (aspseek
http://www.aspseek.org/) compile under gcc 3.4.4 on FC3.
At some point, I found this piece of code:
--
// Dirty hack to avoid...
|
by: sowmiyakc18 |
last post by:
Please clear my doubt. When do we declare a variable to be a register
variable? What is its significance? What are the conditions to be
adhered to when register variables are passed between...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |