简体   繁体   中英

Removing allocated space for Table - SQL Server

I got a table where in got allocated space of 3gig but have no rows.
How to remove this allocated space ?

There are some restriction with TRUNCATE TABLE, and if can't do a truncate then you can rebuild your clustered index to free up the allocated space.

You cannot use TRUNCATE TABLE on tables that:

  • Are referenced by a FOREIGN KEY constraint. (You can truncate a table that has a foreign key that references itself.)
  • Participate in an indexed view.
  • Are published by using transactional replication or merge replication.

If the table contains an identity column, the counter for that column is reset to the seed value defined for the column. If no seed was defined, the default value 1 is used. To retain the identity counter, use DELETE instead. If this is the case then you need to reseed the table to continue the identity increment from where it was.

If the table does not have a clustered index you can use TABLOCK hint in your DELETE statement to free up the allocated space.

DELETE FROM table WITH (TABLOCK)

If table is using a clustered index you can rebuild the index to free the space.

You can do:

truncate table [table_name]

Then in Sql Server Express Manager, right click on the database and go to tasks->shrink->database/files (do both). Should clear you up.

These should do it, they will shrink your database files. If the table is empty the reserved space for the table will be released with the following commands

DBCC SHRINKDATABASE ('DBName')
DBCC SHRINKFILE ('LogicalFileName')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM