Showing posts with label Truncate Database. Show all posts
Showing posts with label Truncate Database. Show all posts

Friday, 9 February 2018

Truncate Database All Table In SqlServer

DECLARE @tablas TABLE(tableName nvarchar(100))

INSERT INTO @tablas
SELECT t.TABLE_SCHEMA+ '.'+t.TABLE_NAME FROM INFORMATION_SCHEMA.TABLES T

DECLARE @loopCounter INT
SELECT @loopCounter = 0
SELECT @loopCounter = COUNT(*) FROM INFORMATION_SCHEMA.TABLES

WHILE @loopCounter>0 
BEGIN
    SELECT TOP 1 @tableName = tableName FROM @tablas 
    DECLARE @sql NVARCHAR(500) = ''

    SET @sql = @sql +'Truncate table  '+ @tableName
    EXECUTE (@sql)
    SELECT @sql
    SET @loopCounter = @loopCounter-1   
    DELETE TOP (1) @tablas 
END

Get all non-clustered indexes

DECLARE cIX CURSOR FOR     SELECT OBJECT_NAME(SI.Object_ID), SI.Object_ID, SI.Name, SI.Index_ID         FROM Sys.Indexes SI             ...