Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is parameterized on database and schema. If no schema is supplied, it will clear all data in the specified database. </p> <p>Handles tables with foreign key references appropriately by disabling constraints. If the procedure fails, which it shouldn't normally do, ensure that you run it successfully after fixing the cause of the problem, which should ensure constraint checking goes back to normal.</p> <p>This will not handle foreign key references correctly if you have foreign keys between schemas, however, it could be fairly easily amended to handle this.</p> <pre><code>create procedure [removeData] (@database_name sysname, @schema_name sysname = null) as set nocount on create table #tables ( TableName varchar(900) not null primary key, HasFKRef bit not null ); declare @sql nvarchar(4000), @table_name varchar(900); if (db_id(@database_name) is null) raiserror ('You must at least specify the database name', 16, 1); set @sql = 'select ''['' + TABLE_CATALOG + ''].['' + TABLE_SCHEMA + ''].['' + TABLE_NAME + '']'' as TableName, (case when exists(select * from [' + @database_name + '].INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc inner join [' + @database_name + '].INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc on rc.UNIQUE_CONSTRAINT_CATALOG = tc.CONSTRAINT_CATALOG and rc.UNIQUE_CONSTRAINT_SCHEMA = tc.CONSTRAINT_SCHEMA and rc.UNIQUE_CONSTRAINT_NAME = tc.CONSTRAINT_NAME where tc.TABLE_NAME = t.TABLE_NAME) then 1 else 0 end) as HasFKRef from [' + @database_name + '].INFORMATION_SCHEMA.TABLES t where TABLE_TYPE = ''BASE TABLE'' and TABLE_SCHEMA = isnull(@schema_name, TABLE_SCHEMA)'; insert into #tables exec sp_executesql @sql, N'@schema_name sysname', @schema_name; declare @curse cursor set @curse = cursor fast_forward for select sql from ( select 'alter table ' + TableName + ' nocheck constraint all' as sql, 1 as sort from #tables union all select 'truncate table ' + TableName, 2 as sort from #tables where HasFKRef = 0 union all select 'delete from ' + TableName, 3 as sort from #tables where HasFKRef = 1 union all select 'alter table ' + TableName + ' with check check constraint all', 4 as sort from #tables ) t order by sort, sql open @curse fetch next from @curse into @sql while (@@fetch_status = 0) begin exec (@sql) fetch next from @curse into @sql end close @curse GO </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload