Hello all,
I have recently been working on a project that requires one simple table to insert data into. The problem here is that all the data inserted must only access the database via stored procedure and I want to ensure that no duplicate data is inserted in the database.
I have done quite a bit of research for many ways to perform duplicate data testing from building temp tables and on, but nothing has really stood out to me yet. I would really like to find some information on how to perform duplicate data testing using a stored procedure that allows to test the data being inserted before it is saved to the database; therefore, when the user inserts the fields and clicks the insert button, the fields will be tested against the existing data (via stored procedure) within the database before being added.
Can anyone help?
ThanksCould you provide us the DDL?|||Well lets see...
I am not an expert in database programming but I am learning...
Ok,
here is some psuedocode for a general idea
create procedure insertData
(
var 1,
var 2,
var 3
)
set nocount on
as
insert into <table> values <1, 2, 3>
select * from <table>
for(i < list) --perform test and search the database
(
array < list> or perhaps a resursion statement
if (list == var1, var2, var3)
return -1
else return 0
)|||you don't have to check for duplicates, the database can do this for you automatically
just declare a unique constraint on the column(s) that you want to be unique
vwalah!
:)|||well thanks,
that was easy!|||There are several ways to prevent duplicate data from being inserted into a table. One of them would be using a UNIQUE constraint.
But, before you get into the solution I'd like to ask you to provide us with the real table structure, some sample data, as well as the business rules you are trying to enforce. All this because there's also the chance that you are using an inadequate table design to fullfill your needs.|||This is just one table with the fields: year, month, product type, product amount
Now the table does not have a unique field, product type is a listing of only five categories such as: apples, pears, peaches, grapes, and oranges
So really there is no unique field.|||my opinion: year, month, product type are unique, and should be defined as the primary key
vwalah! no need to check for dupes, the database will do it for you :)|||But you could have data stating the same year, month, and product type, right?|||since this is just one table why not set all of the fields as a primary key?|||*shakes head*
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment