{"id":182,"date":"2012-04-27T11:01:27","date_gmt":"2012-04-27T11:01:27","guid":{"rendered":"https:\/\/sqlserverlearner.azurewebsites.net\/2012\/04\/27\/truncate-all-tables-sql-server-2008\/"},"modified":"2012-04-27T11:01:27","modified_gmt":"2012-04-27T11:01:27","slug":"truncate-all-tables-sql-server-2008","status":"publish","type":"post","link":"https:\/\/sqlserverlearner.com\/2012\/04\/27\/truncate-all-tables-sql-server-2008\/","title":{"rendered":"truncate all tables sql server 2008"},"content":{"rendered":"

How to truncate all tables sql server?<\/strong><\/p>\n

Following script truncates all the tables in SQL Server:<\/p>\n

[sql]<\/p>\n

DECLARE @tablename AS VARCHAR (1000)<\/p>\n

DECLARE @sql AS VARCHAR (1000)<\/p>\n

IF OBJECT_ID(‘tempdb.dbo.#tables’) IS NOT NULL
\n DROP TABLE #tables<\/p>\n

SELECT *
\nINTO #tables
\nFROM sys.tables<\/p>\n

WHILE EXISTS (SELECT *
\n FROM #tables)
\n BEGIN
\n SELECT @tablename = name
\n FROM #tables
\n SELECT @sql = ‘truncate table ‘ + @tablename;
\n PRINT @sql
\n EXECUTE (@sql)
\n DELETE #tables
\n WHERE name = @tablename;
\n END
\n[\/sql]<\/p>\n

Works if the tables do not have foriegn key constrains or schema binding relations with other tables\/objects in the database.<\/p>\n","protected":false},"excerpt":{"rendered":"

How to truncate all tables sql server? Following script truncates all the tables in SQL Server: [sql] DECLARE @tablename AS VARCHAR (1000) DECLARE @sql AS VARCHAR (1000) IF OBJECT_ID(‘tempdb.dbo.#tables’) IS NOT NULL DROP TABLE #tables SELECT * INTO #tables FROM sys.tables WHILE EXISTS (SELECT * FROM #tables) BEGIN SELECT @tablename = name FROM #tables SELECT…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1789],"tags":[1838,1839,1840,1841,1842,1843,1844],"_links":{"self":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/182"}],"collection":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/comments?post=182"}],"version-history":[{"count":0,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/182\/revisions"}],"wp:attachment":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/media?parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/categories?post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/tags?post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}