Q
Q
Qairat2017-10-24 12:45:12
SQL Server
Qairat, 2017-10-24 12:45:12

How to bulk clear all tables at once in MsSql?

Hello!
How to bulk clear all tables at once in MsSql?
That to each table not to write truncate table.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey Skorzhinsky, 2017-10-24
@AndyKorg

Something like this:

declare @s nvarchar(max)  = ''
select @s  = @s  +
'truncate table '+TABLE_NAME+';'
  from INFORMATION_SCHEMA.TABLES 

exec (@s)

D
d-stream, 2017-10-24
@d-stream

Stupid, but "armor-piercing" option:

DECLARE @execute_cmd varchar(max)= '';

-- выбрать один из трех вариантов по вкусу
--SELECT @execute_cmd = @execute_cmd+'begin try  truncate table ['+name+'] end try  begin catch end catch ' FROM sys.tables WHERE type_desc = 'USER_TABLE';

--SELECT @execute_cmd = @execute_cmd+'begin try  truncate table ['+TABLE_NAME+'] end try  begin catch end catch ' FROM information_schema.tables WHERE TABLE_TYPE = 'BASE TABLE';

--SELECT @execute_cmd = @execute_cmd+'begin try  truncate table ['+Name+'] end try  begin catch end catch ' FROM dbo.sysobjects WHERE xtype = 'U';

EXECUTE @execute_cmd;

and then repeatedly "hollow" the last line ....
because foreign key and just delete the table referenced by data from another table is not an option

K
krypt3r, 2017-10-25
@krypt3r

DROP DATABASE dbname;
Joke :-D

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question