Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Drop and Create table or Trunc table


Drop and Create table or Trunc table

Author
Message
Jim1234
Jim1234
Valued Member
Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)

Group: General Forum Members
Points: 61 Visits: 139
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
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16632 Visits: 17024
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47343 Visits: 44392
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


SQL_Buddy99
SQL_Buddy99
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 398
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
Honny
Honny
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 107
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.
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45241 Visits: 39927
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
wolfkillj
wolfkillj
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1104 Visits: 2582
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
Blog: SQLSouth
Twitter: @SQLSouth
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45241 Visits: 39927
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
wolfkillj
wolfkillj
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1104 Visits: 2582
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
Blog: SQLSouth
Twitter: @SQLSouth
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45241 Visits: 39927
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search