Drop and Create table or Trunc table

  • Hi All,

    I have a scenario in which I have to delete rows in a MS SQL Server table and load it. The rows will be approx. 200,000 with 10 columns.

    I'm thinking of two options. 1) Truncate rows and load. 2) Drop and Create table and load. It does not have any Foreign key. Which one is good?

    Please suggest..

    Cheers

    Jim

  • Truncate and load.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • They'll both do pretty much the same thing, the DROP/CREATE is more typing and more prone to typing errors.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • You can do some thing like this

    select * into TempTable

    from OldTable

    -- Here you can use a where condition to get only the records you wanted.

    --or you can delete the records in the TempTable

    DROP TABLE OldTable

    EXEC sp_rename 'TempTable','OldTable'

    This will get your table back with the columns you want

  • As you mentioned there is no Foreign Keys so truncate and load is better.

    1. If you drop the table again you have to create the table and load.

    2. You have drop the Table if there is any changes in he table structure from previous load that time it is better way but in your case truncate and load is better.

  • Honny (11/11/2013)


    As you mentioned there is no Foreign Keys so truncate and load is better.

    1. If you drop the table again you have to create the table and load.

    2. You have drop the Table if there is any changes in he table structure from previous load that time it is better way but in your case truncate and load is better.

    I don't believe so. I believe that the SELECT/INTO solution that "SQL_Learning" proposed will do the trick and no modifications to the table will be required if the source-query ever changes.

    --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)

  • Jeff Moden (11/11/2013)


    Honny (11/11/2013)


    As you mentioned there is no Foreign Keys so truncate and load is better.

    1. If you drop the table again you have to create the table and load.

    2. You have drop the Table if there is any changes in he table structure from previous load that time it is better way but in your case truncate and load is better.

    I don't believe so. I believe that the SELECT/INTO solution that "SQL_Learning" proposed will do the trick and no modifications to the table will be required if the source-query ever changes.

    I don't think SQL_Learning's suggestion meets the requirement. It sounds like Jim1234 wants to do is flush ALL the data from a table and replace it with entirely new data. TRUNCATE and INSERT (or BULK INSERT or other means of populating a table) accomplishes that goal. SQL_Learning's suggestion fits the requirement to delete some rows while retaining others from a table.

    Now, as to whether truncating the table and reloading it entirely anew is the best choice to meet the *business* requirements, I don't know - Jim1234 didn't provide enough information about his process to determine whether he really ought to be truncating and reloading or doing an INSERT/UPDATE.

    Jason Wolfkill

  • wolfkillj (11/12/2013)


    Jeff Moden (11/11/2013)


    Honny (11/11/2013)


    As you mentioned there is no Foreign Keys so truncate and load is better.

    1. If you drop the table again you have to create the table and load.

    2. You have drop the Table if there is any changes in he table structure from previous load that time it is better way but in your case truncate and load is better.

    I don't believe so. I believe that the SELECT/INTO solution that "SQL_Learning" proposed will do the trick and no modifications to the table will be required if the source-query ever changes.

    I don't think SQL_Learning's suggestion meets the requirement. It sounds like Jim1234 wants to do is flush ALL the data from a table and replace it with entirely new data. TRUNCATE and INSERT (or BULK INSERT or other means of populating a table) accomplishes that goal. SQL_Learning's suggestion fits the requirement to delete some rows while retaining others from a table.

    Now, as to whether truncating the table and reloading it entirely anew is the best choice to meet the *business* requirements, I don't know - Jim1234 didn't provide enough information about his process to determine whether he really ought to be truncating and reloading or doing an INSERT/UPDATE.

    So why would you think that dropping the table and rebuilding it using SELECT/INTO wouldn't fit the bill?

    --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)

  • Jeff Moden (11/13/2013)


    wolfkillj (11/12/2013)


    Jeff Moden (11/11/2013)


    Honny (11/11/2013)


    As you mentioned there is no Foreign Keys so truncate and load is better.

    1. If you drop the table again you have to create the table and load.

    2. You have drop the Table if there is any changes in he table structure from previous load that time it is better way but in your case truncate and load is better.

    I don't believe so. I believe that the SELECT/INTO solution that "SQL_Learning" proposed will do the trick and no modifications to the table will be required if the source-query ever changes.

    I don't think SQL_Learning's suggestion meets the requirement. It sounds like Jim1234 wants to do is flush ALL the data from a table and replace it with entirely new data. TRUNCATE and INSERT (or BULK INSERT or other means of populating a table) accomplishes that goal. SQL_Learning's suggestion fits the requirement to delete some rows while retaining others from a table.

    Now, as to whether truncating the table and reloading it entirely anew is the best choice to meet the *business* requirements, I don't know - Jim1234 didn't provide enough information about his process to determine whether he really ought to be truncating and reloading or doing an INSERT/UPDATE.

    So why would you think that dropping the table and rebuilding it using SELECT/INTO wouldn't fit the bill?

    Jeff Moden (11/13/2013)


    wolfkillj (11/12/2013)


    Jeff Moden (11/11/2013)


    Honny (11/11/2013)


    As you mentioned there is no Foreign Keys so truncate and load is better.

    1. If you drop the table again you have to create the table and load.

    2. You have drop the Table if there is any changes in he table structure from previous load that time it is better way but in your case truncate and load is better.

    I don't believe so. I believe that the SELECT/INTO solution that "SQL_Learning" proposed will do the trick and no modifications to the table will be required if the source-query ever changes.

    I don't think SQL_Learning's suggestion meets the requirement. It sounds like Jim1234 wants to do is flush ALL the data from a table and replace it with entirely new data. TRUNCATE and INSERT (or BULK INSERT or other means of populating a table) accomplishes that goal. SQL_Learning's suggestion fits the requirement to delete some rows while retaining others from a table.

    Now, as to whether truncating the table and reloading it entirely anew is the best choice to meet the *business* requirements, I don't know - Jim1234 didn't provide enough information about his process to determine whether he really ought to be truncating and reloading or doing an INSERT/UPDATE.

    So why would you think that dropping the table and rebuilding it using SELECT/INTO wouldn't fit the bill?

    I don't necessarily think that. As I understood it, SQL_Learning proposed this process, which is great when one wants to keep some of the existing data:

    1. SELECT * FROM ExistingTable INTO NewTable WHERE (some conditions that define which rows should not be deleted)

    2. DxROP TABLE ExistingTable

    3. Rename NewTable to ExistingTable.

    As I understood it, Jim1234 doesn't want to keep ANY rows from ExistingTable - he just wants to replace that data with new data. SQL_Learning's suggestion solves a different problem.

    As an aside, a process involving a DxROP TABLE step isn't possible if there are any schema-bound dependencies on the table - the dependent objects have to dropped first. TRUNCATE is unaffected by schema-bound dependencies. If what you need to do is flush the data from a table in preparation for replacing it with new data, truncating instead of dropping the table means the process doesn't break when someone decides they need an indexed view over the table.

    Jason Wolfkill

  • wolfkillj (11/13/2013)


    Jeff Moden (11/13/2013)


    wolfkillj (11/12/2013)


    Jeff Moden (11/11/2013)


    Honny (11/11/2013)


    As you mentioned there is no Foreign Keys so truncate and load is better.

    1. If you drop the table again you have to create the table and load.

    2. You have drop the Table if there is any changes in he table structure from previous load that time it is better way but in your case truncate and load is better.

    I don't believe so. I believe that the SELECT/INTO solution that "SQL_Learning" proposed will do the trick and no modifications to the table will be required if the source-query ever changes.

    I don't think SQL_Learning's suggestion meets the requirement. It sounds like Jim1234 wants to do is flush ALL the data from a table and replace it with entirely new data. TRUNCATE and INSERT (or BULK INSERT or other means of populating a table) accomplishes that goal. SQL_Learning's suggestion fits the requirement to delete some rows while retaining others from a table.

    Now, as to whether truncating the table and reloading it entirely anew is the best choice to meet the *business* requirements, I don't know - Jim1234 didn't provide enough information about his process to determine whether he really ought to be truncating and reloading or doing an INSERT/UPDATE.

    So why would you think that dropping the table and rebuilding it using SELECT/INTO wouldn't fit the bill?

    Jeff Moden (11/13/2013)


    wolfkillj (11/12/2013)


    Jeff Moden (11/11/2013)


    Honny (11/11/2013)


    As you mentioned there is no Foreign Keys so truncate and load is better.

    1. If you drop the table again you have to create the table and load.

    2. You have drop the Table if there is any changes in he table structure from previous load that time it is better way but in your case truncate and load is better.

    I don't believe so. I believe that the SELECT/INTO solution that "SQL_Learning" proposed will do the trick and no modifications to the table will be required if the source-query ever changes.

    I don't think SQL_Learning's suggestion meets the requirement. It sounds like Jim1234 wants to do is flush ALL the data from a table and replace it with entirely new data. TRUNCATE and INSERT (or BULK INSERT or other means of populating a table) accomplishes that goal. SQL_Learning's suggestion fits the requirement to delete some rows while retaining others from a table.

    Now, as to whether truncating the table and reloading it entirely anew is the best choice to meet the *business* requirements, I don't know - Jim1234 didn't provide enough information about his process to determine whether he really ought to be truncating and reloading or doing an INSERT/UPDATE.

    So why would you think that dropping the table and rebuilding it using SELECT/INTO wouldn't fit the bill?

    I don't necessarily think that. As I understood it, SQL_Learning proposed this process, which is great when one wants to keep some of the existing data:

    1. SELECT * FROM ExistingTable INTO NewTable WHERE (some conditions that define which rows should not be deleted)

    2. DxROP TABLE ExistingTable

    3. Rename NewTable to ExistingTable.

    As I understood it, Jim1234 doesn't want to keep ANY rows from ExistingTable - he just wants to replace that data with new data. SQL_Learning's suggestion solves a different problem.

    As an aside, a process involving a DxROP TABLE step isn't possible if there are any schema-bound dependencies on the table - the dependent objects have to dropped first. TRUNCATE is unaffected by schema-bound dependencies. If what you need to do is flush the data from a table in preparation for replacing it with new data, truncating instead of dropping the table means the process doesn't break when someone decides they need an indexed view over the table.

    I believe you might be a little confused as to what SELECT/INTO does. It does not work on an existing table. It's to build a brand new table and populat it on the fly. Nothing old is kept.

    Also, TRUNCATE is certainly affected by dependancies. For example, FK's can be a real problem for Truncate. I do agree about the indexed view over the table causing a problem with a DROP, though.

    --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 10 posts - 1 through 9 (of 9 total)

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