Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress the output of `DBCC SHRINKFILE`?

Tags:

tsql

In order to hide the output of DBCC SHRINKFILE I tried:

CREATE TABLE #Swallow(DbId int, Fileld int, CurrentSize int, MininumSize int, UsedPages int, EstimatedPages int)
INSERT INTO #Swallow EXEC('DBCC SHRINKFILE(''filename'', 0, TRUNCATEONLY)')

but it returns the following error:

Cannot perform a shrinkfile operation inside a user transaction. Terminate the transaction and reissue the statement.

How can this be done?

like image 837
Ant_222 Avatar asked Oct 12 '15 13:10

Ant_222


People also ask

How do I shrink a file in DBCC?

DBCC SHRINKFILE (Transact-SQL) Shrinks the size of the specified data or log file for the current database, or empties a file by moving the data from the specified file to other files in the same filegroup, allowing the file to be removed from the database. You can shrink a file to a size that is less than the size specified when it was created.

What happens when you shrink a database data file?

If you shrink a database data file with NOTRUNCATE which causes massive index fragmentation and then a SHRINKFILE operation followed by an index rebuild this will push the database back to the original size. I am not an advocate of shrinking SQL Server database data files unless it is absolutely necessary.

What is the use of the shrink command?

Shrinks the current database's specified data or log file size. You can use it to move data from one file to other files in the same filegroup, which empties the file and allows for its database removal.

Does shrinkfile truncate the size of a file?

The file size hadn’t changed at all! You see, SHRINKFILE doesn’t truncate the file until the desired size is reached or there is no more free space to squeeze out. Checkpoints?


1 Answers

It seems there's a WITH NO_INFOMSGS option:

DBCC SHRINKFILE('filename', 0, TRUNCATEONLY) WITH NO_INFOMSGS

Ref: http://ss64.com/sql/dbcc_shrinkfile.html

like image 134
JohnLBevan Avatar answered Oct 28 '22 15:10

JohnLBevan