wHICH IS BEST TO USE tRUNCATE OR dROP

  • Hi,

    I am using Drop and create table in Loop because of results I want to achieve using loop. But my question is

    Is this better to use Truncate table rather than Drop and creating table again.

  • Rakesh.Chaudhary (10/24/2013)


    Hi,

    I am using Drop and create table in Loop because of results I want to achieve using loop. But my question is

    Is this better to use Truncate table rather than Drop and creating table again.

    If you need a clean table every time you enter in loop, just TRUNCATE it.

  • Rakesh.Chaudhary (10/24/2013)


    Hi,

    I am using Drop and create table in Loop because of results I want to achieve using loop. But my question is

    Is this better to use Truncate table rather than Drop and creating table again.

    Best is probably to not loop 😀


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • From a core behavior stand point, there's no real difference between the two. Both are basically just deallocating the resource. But, you then have to go back and recreate the structure after a drop.

    Why would you truncate in a loop?

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • I have to calculate the amonts for Previos day ,SO I am using where

    condition

    WHILE ( @DateMinRes <= @DateMaxRes )

    BEGIN

    INSERT INTO tABLE A

    (

    a,

    b,

    c

    )

    SELECT

    a,

    b,

    c

    FROM xyz

    Where Day < @DateMinRes

    SELECT SUM(a) from Table A

    DRop Table A

    SET@DateMin = @DateMin +1

    END

    ----------AS in above query I have to calculate sum from table A for previous day and thats why i am dropping it and creating again

  • I'm not sure you are asking the right question. I don't believe you need to drop tables or loop through the data. It seems like you would be able to handle this in a single query using a CTE possibility. If you can provide a script to create the necessary tables and another script to populate the tables and then provide us with the desired outcome someone maybe able to help you with a query to handle this case.

    EDIT:

    After reading the last post again you may be able to simply add a GROUP BY on the date with a sum i.e.:

    SELECT

    sum(a)

    b,

    c

    FROM xyz

    Where Day >= @DateMinRes

    and Day < @DateMaxRes

    group by Day, b, c



    Microsoft Certified Master - SQL Server 2008
    Follow me on twitter: @keith_tate

    Forum Etiquette: How to post data/code on a forum to get the best help[/url]

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

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