{"id":8760,"date":"2022-05-16T06:03:00","date_gmt":"2022-05-16T04:03:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=8760"},"modified":"2022-07-13T12:21:23","modified_gmt":"2022-07-13T10:21:23","slug":"empty-sql-server-table-truncate","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/t-sql\/empty-sql-server-table-truncate\/","title":{"rendered":"Empty a SQL Server table (TRUNCATE)"},"content":{"rendered":"\n<p>How do I clear all the data in a SQL Server table with a query? From SSMS, connect to the SQL Server database and use the TRUNCATE TABLE command to clear the data from the table.<\/p>\n\n\n\n<p>Note that the TRUNCATE command is different from the DELETE query in that all data is removed without any filter. The TRUNCATE command is therefore faster and recommended.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-empty-a-sql-server-table-with-the-truncate-table-query\">Empty a SQL Server table with the Truncate Table query<\/h2>\n\n\n\n<p>This command deletes all the data stored in the table. Be careful, this command is irreversible. Remember to check your database backups to avoid losing data.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">Note that this action is irreversible.<\/mark><\/strong><\/p>\n\n\n\n<p>Simply use a T-SQL query from <strong><a href=\"https:\/\/expert-only.com\/en\/ssms\/download-sql-server-management-studio-18-ssms\/\">SQL Server Management Studio<\/a><\/strong>, with the TRUNCATE TABLE command.<\/p>\n\n\n\n<p>Note that the <a href=\"https:\/\/www.geeksforgeeks.org\/difference-between-delete-and-truncate\/\" target=\"_blank\" rel=\"noreferrer noopener\">TRUNCATE query is different from the DELETE<\/a> command, especially in terms of performance. A DELETE FROM command scans all rows before deleting and allows you to filter the rows to be deleted.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">TRUNCATE TABLE [dbo][SALES];\nGO\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Minimum rights needed on the table to execute a TRUNCATE<\/h3>\n\n\n\n<p>The TRUNCATE command has more impact than a simple DELETE, especially because the truncate cannot be undone via the <strong><a href=\"https:\/\/expert-only.com\/en\/sql-server-db\/empty-the-sql-server-transaction-log-and-fix-error-9002\/\">transaction log<\/a><\/strong>. The minimum right to execute a TRUNCATE command is ALTER. Because the DBMS considers this to be a modification of the table.<\/p>\n\n\n\n<p>To delete the table completely, the data structure and the data must be deleted. To avoid generating error messages, then <strong><a href=\"https:\/\/expert-only.com\/en\/t-sql\/check-if-table-exists-in-sql-server\/\">test the existence of the SQL Server table before deleting it<\/a><\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-tutoriels-et-exemples-sql-server-et-microsoft-it wp-block-embed-tutoriels-et-exemples-sql-server-et-microsoft-it\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"Fj0ItFKdl0\"><a href=\"https:\/\/expert-only.com\/en\/t-sql\/check-if-table-exists-in-sql-server\/\">Check if table exists in SQL Server<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Check if table exists in SQL Server&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/t-sql\/check-if-table-exists-in-sql-server\/embed\/#?secret=LuVN2mmTJV#?secret=Fj0ItFKdl0\" data-secret=\"Fj0ItFKdl0\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>How do I clear all the data in a SQL Server table with a query? From SSMS, connect to the SQL Server database and use the TRUNCATE TABLE command to clear the data from the table. Note that the <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/t-sql\/empty-sql-server-table-truncate\/\" title=\"Empty a SQL Server table (TRUNCATE)\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":6041,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[454],"tags":[],"class_list":{"0":"post-8760","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-t-sql"},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/8760","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=8760"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/8760\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/6041"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=8760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=8760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=8760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}