What would be right approach?

  • 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?

  • Why don't you just use UPDATE (if all 31 rows are present) or MERGE (if they are not)?


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    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

  • 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

  • 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 mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    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

  • May be that:

    SELECTDATEADD (DAY,sv.Number,'20120801')

    FROMmaster.dbo.spt_values sv

    WHEREsv.Name IS NULL AND sv.Number < 31

  • 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


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply