Afternoon All,
I have a button on a web page that when is clicked by the user inserts data into a table. this button has a message box assigned to it, to let the user know the records have been saved and then redirects the user to the home page. This in its current state works perfectly fine.
My problem is that I wish to find a solution to stop the user going back to the page where this button is located and inserting the data again. I need to get round having duplicate records in the database table.
I’m presuming that from what I have read on the internet that I should be using the ‘IF EXISTS’ function within my stored procedure. I’m not 100% sure how this will work with the current code that I have in my Stored Procedure? I’m only want to check that these records are available and if not then to add add them or if they already exist then not to add these.
I just think that I have confused myself slightly with the amount of articles etc that I have read on the internet.
Here is my current stored procedure:
ALTER PROCEDURE [dbo].[GasNominationsRawData_Insert]
AS SET NOCOUNT ON;
INSERT INTO dbo.GasRawData (timestamp,TagName,Value)
SELECT timestamp AS Interval, Left(Right(TagName,Len(TagName)-5),Len(TagName)-10) As TagName,
CONVERT(decimal(10, 2), ROUND(value, 2)) As Value
FROM
OPENQUERY(IHISTORIAN,'
SET starttime =''yesterday +4h'', endtime =''today +6h''
SELECT timestamp, tagname, value
FROM ihRawData
WHERE tagname = "UMIS.99FC9051.F_CV"
OR tagname = "UMIS.99F851C.F_CV"
OR tagname = "UMIS.35GTGAS.F_CV"
OR tagname = "UMIS.99XXG546.F_CV"
AND timestamp BETWEEN ''timestamp'' and ''timestamp''
AND SamplingMode =Calculated
AND CalculationMode =Average
AND IntervalMilliseconds =1h
ORDER BY tagname, timestamp
')
Any help or suggestions would be much appreciated.
Regards Betty.
One very simple way of preventing duplicates is to add a
UNIQUE INDEXon the field(s) you want to keep distinctive.CREATE UNIQUE INDEX ix_MyIndexName ON Table(Field1, Field2, Field3)This will throw an error if you try to insert a duplicate value for the fields listed.
Alternatively, you can add the option
IGNORE_DUP_KEY = ONto the above to continue if a duplicate value is attempted to be inserted. A warning will be generated but it won’t cause the query to fail.This has minimal overhead and won’t require you to do any maintenance work.