Solved

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

Posted on 2011-03-15
8
594 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
 
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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

863 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

26 Experts available now in Live!

Get 1:1 Help Now