How to delete the records of several tables by scoring the ones that should not be deleted in sql server 2014?

Asked

Viewed 42 times

0

I would like to delete all the records from the tables of my database, but some tables cannot be deleted, here in my case for example I have an average of 20 tables of which 6 cannot be erased the records. Is there any way to do that?

1 answer

0


You can recover the name of all the tables in the database and run the delete command. example:

DECLARE @sql NVARCHAR(max)=''

SELECT @sql += ' DELETE FROM ' + QUOTENAME(s.NAME) + '.' + QUOTENAME(t.NAME) + '; '
FROM   sys.tables t
       JOIN sys.schemas s
         ON t.[schema_id] = s.[schema_id]
WHERE  t.type = 'U' AND t.NAME NOT IN ('','','','')

Exec sp_executesql @sql

Watch the line: AND t.NAME NOT IN ('','','','') inside the simple quotes you should put the name of the tables that DO NOT WANT TO DELETE the data.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.