Solved

can not truncate sql table, nor drop the index of a tab

Posted on 2011-03-15
8
581 Views
Last Modified: 2012-05-11
I have a table in the SQL data base, current have just over 1 million rows.
1. I need to create a new index, but it runs and run forever., I have to abort it.
2.  but I can use bcp utility to dump all the rows to a text file.
3. I I can also use ado recordset to insert new row to the table
4. I can do everything with the table, other than I can not truncate table (I intend to create the index    after I truncate it. then use bcp to reload the data.

can someone help?
0
Comment
Question by:mshox1
8 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
Comment Utility
>I intend to create the index    after I truncate it. then use bcp to reload the data.
i dont recommend that.
Do you think any other application is using the table at the same time you creat the index?
>but it runs and run forever., I have to abort it.

i may take some time depending on your index , datattype, free space etc...
0
 
LVL 11

Assisted Solution

by:JoeNuvo
JoeNuvo earned 100 total points
Comment Utility
if table is not very busy with insert.
you may create new table, create whatever index you want
transfer existing data into new table
and then rename it.

exec sp_rename 'MyTable','MyTable_Old'
exec sp_rename 'MyTable_New','MyTable'

Open in new window

0
 
LVL 4

Assisted Solution

by:qasim_md
qasim_md earned 300 total points
Comment Utility
else try this :::

WHILE (SELECT COUNT(*) FROM table1) > 0
BEGIN
DELETE TOP(n) /*top n rows*/
FROM table1
END
GO
0
 
LVL 4

Accepted Solution

by:
qasim_md earned 300 total points
Comment Utility
Another Option:
1) create a new table with all the indexes and then do a
INSERT into new_table
select * from old_table;
2) Then rename the table to
sp_rename 'old_table' ,'old_table_old'
sp_rename 'new_table' ,'old_table
3) Verify if evertying is perfect and drop the table old_table_old
4) Shrink the database to free up the space occupied.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 5

Assisted Solution

by:ThakurVinay
ThakurVinay earned 100 total points
Comment Utility
raname the table would be good option, also make a partition on the table for better performance.

my one cent.

Thanx.
Vinay
0
 

Author Comment

by:mshox1
Comment Utility
to :gaism_md

Your suggestions of the above, is very interesting.
I copied below. and I have a questions about #4
==== you wrote the following ====
1) create a new table with all the indexes and then do a
INSERT into new_table
select * from old_table;
   
2) Then rename the table to
sp_rename 'old_table' ,'old_table_old'
sp_rename 'new_table' ,'old_table
3) Verify if evertying is perfect and drop the table old_table_old
4) Shrink the database to free up the space occupied.

questions is how do you "shrink the database t ofree up the sapce occupied"
what is the query or command to perform the above?   Please advise

-- I will try later and confirm if your approach works.

thanks

also:  Thakur Vinay:
are you tell me when you rename the table, it will make apartion o nthe table for better performance?
or do you have a seperate command or query I can do to make new partition on a given table?

Please clarify.  thanks
0
 
LVL 4

Assisted Solution

by:qasim_md
qasim_md earned 300 total points
Comment Utility
To shrink a database :::
Go to SQL server management Studio > connect to the SQL server > right click on the database the table exists > select TASK > shrink > Files and then click on OK.
0
 

Author Closing Comment

by:mshox1
Comment Utility
excellent experts input. I have resolved my issues and learns some new tricks.  thank you again.
0

Featured Post

Why spend so long doing email signature updates?

Do you spend loads of your time carrying out email signature updates? Not very interesting are they? Don’t let signature updates get you down. Let Exclaimer Cloud - Signatures for Office 365 make managing email signatures a breeze.

Join & Write a Comment

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now