Solved

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

Posted on 2011-03-15
8
603 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
ID: 35144366
>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
ID: 35144369
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
ID: 35144405
else try this :::

WHILE (SELECT COUNT(*) FROM table1) > 0
BEGIN
DELETE TOP(n) /*top n rows*/
FROM table1
END
GO
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 4

Accepted Solution

by:
qasim_md earned 300 total points
ID: 35146641
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
 
LVL 5

Assisted Solution

by:ThakurVinay
ThakurVinay earned 100 total points
ID: 35146677
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
ID: 35146727
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
ID: 35147068
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
ID: 35171401
excellent experts input. I have resolved my issues and learns some new tricks.  thank you again.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Abstract Express Replacement Software 12 23
convert null in sql server 12 31
SQL view 2 26
Need help constructing a conditional update query 16 40
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

786 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