How to Delete rows from a table if try insert same value

I have a stored procedure that accepts @date as a parameter. The sp fills a table with various data that is asscoaited with the date entered as a parameter. How would I go about putting in logic that if you enter the same date twice as a parameter it will delete all the rows that were previously associated with that date in the table and enter the new information for that date?

Right now if Run my stored procedure twice exec sp_test'09-12-2011' It will put the same data in twice. I need it to recognize that the date already exists in the table then delete all the rows associated with it. Then re-enter the new rows of that date.

I am not sure how to go about this. Does this make sense?

more ▼

asked Dec 09, 2011 at 11:41 AM in Default

avatar image

216 29 32 35

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

Before filling the table, your procedure needs a delete statement to empty the table of any information pertaining to the supplied date. Maybe something like delete from dateRelatedTable where dateInTheTable = @date

more ▼

answered Dec 09, 2011 at 12:02 PM

avatar image

25k 3 10 20

That was simple. I was way overthinking it.

Dec 09, 2011 at 12:13 PM sqlnewb

Simple is good.

Dec 09, 2011 at 01:17 PM ThomasRushton ♦♦
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Dec 09, 2011 at 11:41 AM

Seen: 5248 times

Last Updated: Dec 09, 2011 at 11:42 AM

Copyright 2017 Redgate Software. Privacy Policy