Solved

Prevent inserting duplicate values in SQL Server database

Posted on 2014-10-30
3
292 Views
Last Modified: 2014-10-30
Hi Experts,
I'm using the following code to insert values in my table:
INSERT INTO OT_Table (OT_Line, Emp_Id, Timestamp) VALUES ('"& strOT_Line &"','"& strEmp_Id &"','"& strTimeStamp &"')

Open in new window

I would like to prevent insertion if the OT_Line and Emp_Id are the same.
In that case I would just like to update the value of the TimeStamp field.

How can I accomplish that on my classic ASP page?
Thank you for your help
0
Comment
Question by:romsom
3 Comments
 
LVL 19

Accepted Solution

by:
Daniel Van Der Werken earned 400 total points
Comment Utility
This is what I do for my INSERT script:

IF NOT EXISTS(SELECT * FROM OT_Table WHERE OT_Line = @strOT_Line  AND TimeStamp  = @strTimeStamp )
   BEGIN
     INSERT INTO OT_Table(OT_Line, Emp_Id, TimeStamp) VALUES (@strOT_Line, @Emp_Id, @strTimeStamp)
   END
0
 
LVL 39

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 100 total points
Comment Utility
I normally call a stored procedure:

create procedure updOTTable
@Ot_Line int,
@emp_id int,
@time datetime
as

if (select count(*) from ot_table where ot_line = @ot_line and emp_id  = @emp_id )  = 0
   -- insert statement
INSERT INTO OT_Table (OT_Line, Emp_Id, Timestamp) VALUES (@ot_line, @emp_id, @timestamp)
else
   -- update statement
    update ot_table
set timestamp = @time where ot_line = @ot_line and emp_id  = @emp_id



You would then call the stored proc in your code.
0
 

Author Closing Comment

by:romsom
Comment Utility
Thank you very much; it's working now. This is classic ASP so I've used the following code:

IF NOT EXISTS(SELECT * FROM OTEWR WHERE OT_Line = '"& strOT_Line &"' AND Emp_Id  = '"& strEmp_Id &"' ) BEGIN INSERT INTO OTEWR(OT_Line, Emp_Id, PersonAddedOn) VALUES ('"& strOT_Line &"', '"& strEmp_Id &"', '"& strPersonAddedOn &"') END
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now