473,659 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Preventing/Removing duplicates

I have a permission tracking app that I am working on, and I have made the
insert page for it. I am having issues on how to prevent duplicates from
getting entered.

Currently the interface for the app has a mixture of select boxes, list
boxes and checkboxes. The form submits the page to processAIMR.asp and then
does the inserting. I am using a loop to insert a new record for each
checkbox checked or listbox entry selected.

My database table looks something like this,

UID - autoincrementin g primary key
EmpID - the employee who the permission is given to
AuthEmpID - the employee who authorized the permission
AccessOption - the permission that was given (i.e. Internet Access, etc).
This is a foreign key to the AccessOptions table.

My question is concerning efficiency and how I should go about preventing or
removing the duplicates. There are a couple methods that I have thought
about using, but I am not sure which would be best.

1. Create a recordset on each run through the loop to check to see if the
AccessOption is already there for a certain EmpID. I would guess this would
be really hard on the server, but it would work.
2. Throw the recordset into an array, then check it (still kinda unsure how
to do this) on each run through the loop.
3. After the processAIMR.asp page has inserted, it redirects. Upon
redirection I could run a DELETE command that deletes all duplicates.

Which do you think would be the best route? Or maybe you have another idea?

Thanks,
Drew
Nov 9 '05 #1
4 3618
Drew wrote:
I have a permission tracking app that I am working on, and I have
made the insert page for it. I am having issues on how to prevent
duplicates from getting entered.

Currently the interface for the app has a mixture of select boxes,
list boxes and checkboxes. The form submits the page to
processAIMR.asp and then does the inserting. I am using a loop to
insert a new record for each checkbox checked or listbox entry
selected.

My database table looks something like this,

UID - autoincrementin g primary key
EmpID - the employee who the permission is given to
AuthEmpID - the employee who authorized the permission
AccessOption - the permission that was given (i.e. Internet Access,
etc). This is a foreign key to the AccessOptions table.

My question is concerning efficiency and how I should go about
preventing or removing the duplicates. There are a couple methods
that I have thought about using, but I am not sure which would be
best.

1. Create a recordset on each run through the loop to check to see
if the AccessOption is already there for a certain EmpID. I would
guess this would be really hard on the server, but it would work.
Horrible idea. Avoid slow, inefficient recordset loops.
2. Throw the recordset into an array, then check it (still kinda
unsure how to do this) on each run through the loop.
Again, unnecessary.
3. After the processAIMR.asp page has inserted, it redirects. Upon
redirection I could run a DELETE command that deletes all duplicates.
No, why slow down your transaction like this?

Which do you think would be the best route? Or maybe you have
another idea?

The only sure way to prevent duplicates is to create a unique index on the
columns that identify unique rows. In this case, it looks as if you need a
unique index on EmpID and AccessOption. Once you have the index in place you
have the option of attempting to insert data and trapping the error that
results from duplicate key violation. Or you can use a query similar to the
one I proposed in this thread:

http://groups.google.com/group/micro...191629a94f3000
For more specific help, tell us what database you are using.
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Nov 9 '05 #2
I am using SQL Server 2000. I guess that I am a little confused on what is
happening on the link provided, I don't understand why we use the MSObjects
table... I also don't understand the following line of code,

cn.execute InsertTXT,lrecs ,129

If you could explain just a little more in depth I would appreciate it!

Thanks,
Drew

"Bob Barrows [MVP]" <re******@NOyah oo.SPAMcom> wrote in message
news:e6******** ******@tk2msftn gp13.phx.gbl...
Drew wrote:
I have a permission tracking app that I am working on, and I have
made the insert page for it. I am having issues on how to prevent
duplicates from getting entered.

Currently the interface for the app has a mixture of select boxes,
list boxes and checkboxes. The form submits the page to
processAIMR.asp and then does the inserting. I am using a loop to
insert a new record for each checkbox checked or listbox entry
selected.

My database table looks something like this,

UID - autoincrementin g primary key
EmpID - the employee who the permission is given to
AuthEmpID - the employee who authorized the permission
AccessOption - the permission that was given (i.e. Internet Access,
etc). This is a foreign key to the AccessOptions table.

My question is concerning efficiency and how I should go about
preventing or removing the duplicates. There are a couple methods
that I have thought about using, but I am not sure which would be
best.

1. Create a recordset on each run through the loop to check to see
if the AccessOption is already there for a certain EmpID. I would
guess this would be really hard on the server, but it would work.


Horrible idea. Avoid slow, inefficient recordset loops.
2. Throw the recordset into an array, then check it (still kinda
unsure how to do this) on each run through the loop.


Again, unnecessary.
3. After the processAIMR.asp page has inserted, it redirects. Upon
redirection I could run a DELETE command that deletes all duplicates.


No, why slow down your transaction like this?

Which do you think would be the best route? Or maybe you have
another idea?

The only sure way to prevent duplicates is to create a unique index on the
columns that identify unique rows. In this case, it looks as if you need a
unique index on EmpID and AccessOption. Once you have the index in place
you
have the option of attempting to insert data and trapping the error that
results from duplicate key violation. Or you can use a query similar to
the
one I proposed in this thread:

http://groups.google.com/group/micro...191629a94f3000
For more specific help, tell us what database you are using.
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Nov 9 '05 #3
Drew wrote:
I am using SQL Server 2000. I guess that I am a little confused on
what is happening on the link provided, I don't understand why we use
the MSObjects table...
I don't blame you ... that was an Access-specific query. Access requires a
FROM clause, so using the builtin system table MSysObjects is a kludge.
T-SQL does not require this (see why it's important to tell us what database
you are using?) so you can do something like:

insert into yourtable(EmpID ,AuthEmpID,Acce ssOption)
select 12, 38, 4 where not exists (
select * from yourtable where EmpID=12 and AccessOption=4)
I also don't understand the following line of
code,

cn.execute InsertTXT,lrecs ,129

If you could explain just a little more in depth I would appreciate
it!


The ADO documentation can be found here :
http://msdn.microsoft.com/library/en...ireference.asp

If you look up the Execute method. you will see that it accepts 3 arguments:
source - the sql statement/table name/stored procedure name
records affected - a byref argument that will contain the number of records
affected after the query is executed
options - command type and execution option

Now that I know what database you are using, I would revise my response to
suggest you use a stored procedure (you should still create the unique
index).

create procedure InsPermission (
@EmpId int,
@AuthEmpID int,
@AccessOption int) AS
SET NOCOUNT ON
IF NOT EXISTS (select * FROM yourtable where
EmpID= @EmpId and AccessOption= @AccessOption)
INSERT INTO yourtable(EmpID ,AuthEmpID,Acce ssOption)
VALUES(@EmpID,@ AuthEmpID,@Acce ssOption)
ELSE
RETURN -1 --or whatever non-zero number you wish to use

Then, in ASP, if you don't wish to know if a duplicate was attempted to be
inserted, simply do this:

cn.InsPermissio n EmpID, AuthEmpID, AccessOption

If you do wish to know if a duplicate was attempted, then use a command
object and read the return value. For more, read:
http://groups.google.com/groups?hl=e...TNGP12.phx.gbl

Bob Barrows

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Nov 9 '05 #4
Bob... I really appreciate your help with this issue... your knowledge
amazes me!

Thanks a bunch,
Drew
"Bob Barrows [MVP]" <re******@NOyah oo.SPAMcom> wrote in message
news:eT******** ******@TK2MSFTN GP15.phx.gbl...
Drew wrote:
I am using SQL Server 2000. I guess that I am a little confused on
what is happening on the link provided, I don't understand why we use
the MSObjects table...


I don't blame you ... that was an Access-specific query. Access requires a
FROM clause, so using the builtin system table MSysObjects is a kludge.
T-SQL does not require this (see why it's important to tell us what
database
you are using?) so you can do something like:

insert into yourtable(EmpID ,AuthEmpID,Acce ssOption)
select 12, 38, 4 where not exists (
select * from yourtable where EmpID=12 and AccessOption=4)
I also don't understand the following line of
code,

cn.execute InsertTXT,lrecs ,129

If you could explain just a little more in depth I would appreciate
it!


The ADO documentation can be found here :
http://msdn.microsoft.com/library/en...ireference.asp

If you look up the Execute method. you will see that it accepts 3
arguments:
source - the sql statement/table name/stored procedure name
records affected - a byref argument that will contain the number of
records
affected after the query is executed
options - command type and execution option

Now that I know what database you are using, I would revise my response to
suggest you use a stored procedure (you should still create the unique
index).

create procedure InsPermission (
@EmpId int,
@AuthEmpID int,
@AccessOption int) AS
SET NOCOUNT ON
IF NOT EXISTS (select * FROM yourtable where
EmpID= @EmpId and AccessOption= @AccessOption)
INSERT INTO yourtable(EmpID ,AuthEmpID,Acce ssOption)
VALUES(@EmpID,@ AuthEmpID,@Acce ssOption)
ELSE
RETURN -1 --or whatever non-zero number you wish to use

Then, in ASP, if you don't wish to know if a duplicate was attempted to be
inserted, simply do this:

cn.InsPermissio n EmpID, AuthEmpID, AccessOption

If you do wish to know if a duplicate was attempted, then use a command
object and read the return value. For more, read:
http://groups.google.com/groups?hl=e...TNGP12.phx.gbl

Bob Barrows

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Nov 9 '05 #5

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

Similar topics

2
3668
by: Iain | last post by:
Hi I have inherited a web app with the following table structure, and need to produce a table without any duplicates. Email seems like the best unique identifier - so only one of each e-mail address should be in the table. Following http://www.sqlteam.com/item.asp?ItemID=3331 I have been able to get a duplicate count working: select Email, count(*) as UserCount
20
3330
by: Rubinho | last post by:
I've a list with duplicate members and I need to make each entry unique. I've come up with two ways of doing it and I'd like some input on what would be considered more pythonic (or at least best practice). Method 1 (the traditional approach) for x in mylist: if mylist.count(x) > 1:
6
3467
by: M B HONG 20 | last post by:
Hi all - I was wondering if Javascript has a way to easily remove duplicates from a string. For example, if I had a string: "car truck car truck truck tree post post tree" it should turn into: "car truck tree post"
0
2578
by: makthar | last post by:
In your query use DISTINCT SELECT DISTINCT CITY FROM <tablename> WHERE STATE='<state name>'. This will bring only one of each city from the table. >-----Original Message----- >I'm getting a list of addresses from a database and binding them to a >datagrid. I have a dropdown list that allows the user
16
4168
by: tyrfboard | last post by:
I've been searching for awhile now on how to remove duplicates from a table within an Access db and have found plenty of articles on finding or deleting duplicates. All I want to do is remove them from within an SQL query - leaving one of the records behind of course. I have a mailing list comprised of a union query that gets records from two separate tables. I want to be able to run a query that removes one (or more) of the duplicated...
1
11776
by: Colin Spalding | last post by:
My problem is that the 'INSERT INTO' query that sends the records to the table is dynamically compiled in VBA and and the target table has a two column primary key. I have made a number of attempts at getting 'WHERE NOT EXISTS' to cure the problem but so far without success and previous postings have resulted in advice to create an 'ignore duplicates' index. This solved the problem in as much as it allowed the SQL to insert the records...
2
2681
by: sjlung | last post by:
I apologise if this is a trivial question but I have appended three tables in access and within this table, there are duplicate entries. I have tried to set my reference number for this table to be a primary key to ensure duplicates are no longer shown but this changes itself as soon as the tables are all appended together. I appreciate any help you can give me. Thanks alot.
7
3362
by: vsgdp | last post by:
I have a container of pointers. It is possible for two pointers to point to the same element. I want to remove duplicates. I am open to which container is best for this. I thought of using std::set, but my elements do not have a '<' operator, as it does not make sense. I could add an integer index to the data structure so that each element has a unique index associated with it and then override '<' to sort by index, but I am not...
4
13958
by: Mokita | last post by:
Hello, I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script, where I want to eliminate the duplicates in an array (String). for (int k=0; k<myLength; k++){ boolean isthere=false; for (int l=0; l<sizeres; l++){ if (res.equals(my)){
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8535
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8629
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5650
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2757
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.