473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Dirty Read ?

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
Jul 20 '05 #1
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
Jul 20 '05 #2
>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
Jul 20 '05 #3
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)
Jul 20 '05 #4
>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
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
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...
6
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...
15
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...
2
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...
1
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...
9
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...
10
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)...
1
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...
5
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...
28
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...
1
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...
0
isladogs
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...
0
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...
0
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,...
0
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$) { } ...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.