x

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

sqlnewb gravatar image

sqlnewb
216 27 30 31

(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

KenJ gravatar image

KenJ
19.2k 1 3 11

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.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

SQL Server Central

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

Topics:

x1834
x407
x279

asked: Dec 09, 2011 at 11:41 AM

Seen: 3992 times

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