473,473 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

general Q about formulae

Before I go into specifics, this is my problem. I've have a table
that gets updated with large amounts of data on a monthly basis.
Sometimes (rarely) identical rows of data are on one months import that
already exist from the previous month. I can identify these rows from a
combination of two fields (sampleID and testname).
My question is this. Would it be an appropriate 'fix' if I created a
new 'formula' field on the table comprising of a concatentation of
these two fields and then made that an index field (no duplicates)? My
guess would be that if we then tried to import a record with a sampleID
and testname that already existed, then the import for that record
would simply fail.
Would this work? Is there a better way? My background is more with
Access so apologies if I'm not using the right terminology.

Simon Harris

Aug 3 '05 #1
4 2336
You don't mention how you import the source data (from a flat file,
from another table etc.), but assuming that it's in a table - or you
can put it there - then you can INSERT only rows which don't already
exist:

insert into dbo.TargetTable (col1, col2, ...)
select col1, col2, ...
from dbo.SourceTable s
where not exists (
select *
from dbo.TargetTable t
where s.SampleID = t.SampleID
and s.TestName = t.TestName)

This is assuming that (SampleID, TestName) is the primary key of the
target table, which seems to be what you've described.

When you INSERT a lot of rows together, it's usually all or nothing -
if one row violates a unique constraint, the whole INSERT is rolled
back. There can be exceptions to this if you're using bulk import tools
like bcp.exe or DTS which are able to send the 'problem' rows to a
separate file.

Simon

Aug 3 '05 #2
(si**********@kingshc.nhs.uk) writes:
Before I go into specifics, this is my problem. I've have a table
that gets updated with large amounts of data on a monthly basis.
Sometimes (rarely) identical rows of data are on one months import that
already exist from the previous month. I can identify these rows from a
combination of two fields (sampleID and testname).
My question is this. Would it be an appropriate 'fix' if I created a
new 'formula' field on the table comprising of a concatentation of
these two fields and then made that an index field (no duplicates)?
Better yet is just to create a unique index on (sampleID, testname). No
need for formulas.
My guess would be that if we then tried to import a record with a
sampleID and testname that already existed, then the import for that
record would simply fail.


Depends a little on how the import is implemented. But normally that
means that all fails, not just the duplicate. However, there is the
option IGNORE_DUP_KEY for the CREATE INDEX statement. If you add this
option, you will get the behaviour you are looking for.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Aug 3 '05 #3
On 3 Aug 2005 06:42:46 -0700, si**********@kingshc.nhs.uk wrote:
Before I go into specifics, this is my problem. I've have a table
that gets updated with large amounts of data on a monthly basis.
Sometimes (rarely) identical rows of data are on one months import that
already exist from the previous month. I can identify these rows from a
combination of two fields (sampleID and testname).
My question is this. Would it be an appropriate 'fix' if I created a
new 'formula' field on the table comprising of a concatentation of
these two fields and then made that an index field (no duplicates)? My
guess would be that if we then tried to import a record with a sampleID
and testname that already existed, then the import for that record
would simply fail.
Would this work? Is there a better way? My background is more with
Access so apologies if I'm not using the right terminology.


Hi Simon,

Yes, this might work - though (as Simon Hayes already pointed out) it
depends on the used method to import the data if it would refuse only
the duplicated row(s) or roll back the entire operation.

However, there is no need to add this 'formula' column. You can just as
easy define a PRIMARY KEY or UNIQUE constraint on a combination of two
(or more) columns. So in your case, the syntax might be something like:

ALTER TABLE MyTable
ADD CONSTRAINT NoDuplicates UNIQUE (sampleID, testname)

I would definitely add this constraint, if I were yoou. But I would not
rely on the database's error detection and handlig to prevent the
duplicates. I think of constraints as a safety belt - a good driver
makes sure it's never needed; the belt is only there because even the
best driver sometimes makes a mistake. But you don't go crashing into a
brick wall on purpose, because the safety belt will keep you safe.

So in short:
- Incorporate Simon Hayes' suggestion into your code to prevent
insertion of duplicates;
- Add a UNIQUE or PRIMARY KLEY constraint on (sampleID, testname) to
make sure that duplicates will enver be entered by any means;
- Don't add an extra column to store the concatenation of sampleID +
testname, since you don't need it for this purpose.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Aug 3 '05 #4
All,
Thanks for everyones replies. Sadly, (or happily depending on your
viewpoint!) I am not a full-time DBA so I haven't had a chance to implement
your suggestions. Just for info, the import comes from an Access DB
connected through ODBC running automatically, along with several other
export steps, in a DTS package that was created for us by someone who knew
what they were doing who has since moved on. Typical, I know!
If I get any more probs, I know where to come - thanks again!

Simon

"Hugo Kornelis" <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message
news:on********************************@4ax.com...
On 3 Aug 2005 06:42:46 -0700, si**********@kingshc.nhs.uk wrote:
Before I go into specifics, this is my problem. I've have a table
that gets updated with large amounts of data on a monthly basis.
Sometimes (rarely) identical rows of data are on one months import that
already exist from the previous month. I can identify these rows from a
combination of two fields (sampleID and testname).
My question is this. Would it be an appropriate 'fix' if I created a
new 'formula' field on the table comprising of a concatentation of
these two fields and then made that an index field (no duplicates)? My
guess would be that if we then tried to import a record with a sampleID
and testname that already existed, then the import for that record
would simply fail.
Would this work? Is there a better way? My background is more with
Access so apologies if I'm not using the right terminology.


Hi Simon,

Yes, this might work - though (as Simon Hayes already pointed out) it
depends on the used method to import the data if it would refuse only
the duplicated row(s) or roll back the entire operation.

However, there is no need to add this 'formula' column. You can just as
easy define a PRIMARY KEY or UNIQUE constraint on a combination of two
(or more) columns. So in your case, the syntax might be something like:

ALTER TABLE MyTable
ADD CONSTRAINT NoDuplicates UNIQUE (sampleID, testname)

I would definitely add this constraint, if I were yoou. But I would not
rely on the database's error detection and handlig to prevent the
duplicates. I think of constraints as a safety belt - a good driver
makes sure it's never needed; the belt is only there because even the
best driver sometimes makes a mistake. But you don't go crashing into a
brick wall on purpose, because the safety belt will keep you safe.

So in short:
- Incorporate Simon Hayes' suggestion into your code to prevent
insertion of duplicates;
- Add a UNIQUE or PRIMARY KLEY constraint on (sampleID, testname) to
make sure that duplicates will enver be entered by any means;
- Don't add an extra column to store the concatenation of sampleID +
testname, since you don't need it for this purpose.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)

Aug 8 '05 #5

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

Similar topics

1
by: gcook | last post by:
Hi, I've got an old perl program running on my webserver - so old that I haven't used a perl programmer in about two years :) (we've gone all php for a variety of reasons). Anyway, I'm...
1
by: GChong | last post by:
Hi, Im looking for some general 'marketing' info on dotnet: how long it has been in use (i.e first release date), how many companies are using it, number of .NET developers in the world,...
9
by: pankaj_wolfhunter | last post by:
Hi, I need some clearance on the following questions: 1) Does LOAD command updates indexes defined for a table? 2) Is REPLACE option in the LOAD command a logged operation? Help will be...
10
by: Mars | last post by:
if I want to write a program to evaluate a formulae, what kind of algorithm should I use?? for example, input: (2+3)*(3/4)+6-8 how to deal with the brackets?? need to use stacks??
6
by: Andy | last post by:
Someone posted this official proposal to create comp.databases.postgresql.general again. He wrote his own charter. As far as I know, he did not consult any of the postgresql groups first. There...
3
by: Yong | last post by:
I get a general network error when I try to make asynchronously call ExecuteNonQuery on long sql statements that run in parallel. Here is the background info on what I'm trying to accomplish: I...
0
by: Yong | last post by:
I'm not getting any reply to my previous thread so I'm stating a new one. I get a General Network Error due to my SqlCommand object not having a big enough CommandTimeout to complete very long...
0
by: bazzer | last post by:
hey, im trying to access a microsoft access database from an ASP.NET web application in visual basic 2003.NET. i get the following error when i try running it: Server Error in...
10
by: Dixie | last post by:
I am appending some new fields to a table in vba and when I append a number field with is a byte, it does not inherit any format. I want it to be the General Number format, but it is blank. I...
3
by: =?Utf-8?B?Ymxi?= | last post by:
I am posting to the general discussion group - but I cannot find my postings... or replies...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.