{"id":2125,"date":"2017-05-03T09:35:57","date_gmt":"2017-05-03T09:35:57","guid":{"rendered":"http:\/\/galhano.com\/blog\/?p=2125"},"modified":"2017-05-03T09:50:30","modified_gmt":"2017-05-03T09:50:30","slug":"dbcc-shrinkfile-transact-sql","status":"publish","type":"post","link":"http:\/\/galhano.com\/blog\/?p=2125","title":{"rendered":"DBCC SHRINKFILE (Transact-SQL)"},"content":{"rendered":"<h3>A. Shrinking a data file to a specified target size<\/h3>\n<p>The following example shrinks the size of a data file named DataFile1 in the UserDB user database to 7 MB.<\/p>\n<p>[sql]<\/p>\n<p>USE UserDB;<br \/>\nGO<br \/>\nDBCC SHRINKFILE (DataFile1, 7);<br \/>\nGO<\/p>\n<p>[\/sql]<\/p>\n<h3><span style=\"color: #008000;\">B. Shrinking a log file to a specified target size<\/span><\/h3>\n<p>The following example shrinks the log file in the AdventureWorks database to 1 MB. To allow the DBCC SHRINKFILE command to shrink the file, the file is first truncated by setting the database recovery model to SIMPLE.<\/p>\n<p>[sql]<\/p>\n<p>USE AdventureWorks2012;<br \/>\nGO<br \/>\n&#8212; Truncate the log by changing the database recovery model to SIMPLE.<br \/>\nALTER DATABASE AdventureWorks2012<br \/>\nSET RECOVERY SIMPLE;<br \/>\nGO<br \/>\n&#8212; Shrink the truncated log file to 1 MB.<br \/>\nDBCC SHRINKFILE (AdventureWorks2012_Log, 1);<br \/>\nGO<br \/>\n&#8212; Reset the database recovery model.<br \/>\nALTER DATABASE AdventureWorks2012<br \/>\nSET RECOVERY FULL;<br \/>\nGO<\/p>\n<p>[\/sql]<\/p>\n<h3>C. Truncating a data file<\/h3>\n<p>The following example truncates the primary data file in the AdventureWorks database. The sys.database_files catalog view is queried to obtain the file_id of the data file.<\/p>\n<p>[sql]<\/p>\n<p>&amp;nbsp;<\/p>\n<p>USE AdventureWorks2012;<br \/>\nGO<br \/>\nSELECT file_id, name<br \/>\nFROM sys.database_files;<br \/>\nGO<br \/>\nDBCC SHRINKFILE (1, TRUNCATEONLY);<\/p>\n<p>[\/sql]<\/p>\n<h3>D. Emptying a file<\/h3>\n<p>The following example demonstrates the procedure for emptying a file so that it can be removed from the database. For the purposes of this example, a data file is first created and it is assumed that the file contains data.<\/p>\n<p>[sql]<\/p>\n<p>USE AdventureWorks2012;<br \/>\nGO<br \/>\n&#8212; Create a data file and assume it contains data.<br \/>\nALTER DATABASE AdventureWorks2012<br \/>\nADD FILE (<br \/>\nNAME = Test1data,<br \/>\nFILENAME = &#8216;C:\\t1data.ndf&#8217;,<br \/>\nSIZE = 5MB<br \/>\n);<br \/>\nGO<br \/>\n&#8212; Empty the data file.<br \/>\nDBCC SHRINKFILE (Test1data, EMPTYFILE);<br \/>\nGO<br \/>\n&#8212; Remove the data file from the database.<br \/>\nALTER DATABASE AdventureWorks2012<br \/>\nREMOVE FILE Test1data;<br \/>\nGO<\/p>\n<p>[\/sql]<\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/database-console-commands\/dbcc-shrinkfile-transact-sql\">https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/database-console-commands\/dbcc-shrinkfile-transact-sql<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A. Shrinking a data file to a specified target size The following example shrinks the size of a data file named DataFile1 in the UserDB user database to 7 MB. [sql] USE UserDB; GO DBCC SHRINKFILE (DataFile1, 7); GO [\/sql] B. Shrinking a log file to a specified target size The following example shrinks the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"close","ping_status":"close","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,9,104],"tags":[],"class_list":["post-2125","post","type-post","status-publish","format-standard","hentry","category-code-snippets","category-sql-server","category-transact-sql","author-admin"],"_links":{"self":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2125"}],"version-history":[{"count":7,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2125\/revisions"}],"predecessor-version":[{"id":2134,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2125\/revisions\/2134"}],"wp:attachment":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2125"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}