Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-15
8
Medium Priority
?
629 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 400 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 1200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Accepted Solution

by:
qasim_md earned 1200 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 400 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 1200 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

660 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