September 2, 2012 at 9:21 pm
I have a table which contains data for 12 months. Now i want to delete the data for the month of Aug, and insert new data which is present with me. For which i would do the following
delete from MyTable where Cdate between ('2012-08-01','2012-08-31')
Now my question is How to insert the new data into database. Is it like writing 31 insert statement?
September 2, 2012 at 9:29 pm
Why don't you just use UPDATE (if all 31 rows are present) or MERGE (if they are not)?
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
September 2, 2012 at 10:18 pm
dwain.c (9/2/2012)
Why don't you just use UPDATE (if all 31 rows are present) or MERGE (if they are not)?
But that means i have to write update 31 times. Can't this be eliminated by some means
September 2, 2012 at 10:27 pm
Shadab Shah (9/2/2012)
dwain.c (9/2/2012)
Why don't you just use UPDATE (if all 31 rows are present) or MERGE (if they are not)?But that means i have to write update 31 times. Can't this be eliminated by some means
No it does not. You'd need to do something like this:
;WITH MyNewData (a,b,c) AS (
SELECT 1, 2, 3
UNION ALL SELECT 2, 3, 4
-- etc. for 28 more records
UNION ALL SELECT 31, 4, 5)
UPDATE t
SET b=d.b -- new data for column b
,c=d.c -- new data for column c
FROM MyTable t
INNER JOIN MyNewData d
ON d.a = t.a
My column "a" is your "CDate."
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
September 2, 2012 at 10:28 pm
May be that:
SELECTDATEADD (DAY,sv.Number,'20120801')
FROMmaster.dbo.spt_values sv
WHEREsv.Name IS NULL AND sv.Number < 31
September 7, 2012 at 7:46 pm
Shadab Shah (9/2/2012)
I have a table which contains data for 12 months. Now i want to delete the data for the month of Aug, and insert new data which is present with me. For which i would do the following
delete from MyTable where Cdate between ('2012-08-01','2012-08-31')
Now my question is How to insert the new data into database. Is it like writing 31 insert statement?
I'm pretty sure the answer is "NO" but have to make sure... do any of those dates have times other than midnight on them? Also, what is the data-type of the Cdate column?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply