Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. -- Disable all constraints for database
  2. EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"
  3.  
  4. DECLARE @catalog nvarchar(250);
  5. DECLARE @schema nvarchar(250);
  6. DECLARE @tbl nvarchar(250);
  7.  
  8. DECLARE i CURSOR LOCAL FAST_FORWARD FOR select
  9. TABLE_CATALOG,
  10. TABLE_SCHEMA,
  11. TABLE_NAME
  12. from INFORMATION_SCHEMA.TABLES
  13. where
  14. TABLE_TYPE = 'BASE TABLE'
  15. AND TABLE_NAME != 'sysdiagrams'
  16. AND TABLE_NAME != '__RefactorLog'
  17.  
  18. OPEN i;
  19. FETCH NEXT FROM i INTO @catalog, @schema, @tbl;
  20. WHILE @@FETCH_STATUS = 0
  21. BEGIN
  22. DECLARE @sql NVARCHAR(MAX) = N'DELETE FROM [' + @catalog + '].[' + @schema + '].[' + @tbl + '];'
  23. /* Make sure these are the commands you want to execute before executing */
  24. PRINT 'Executing statement: ' + @sql
  25. EXECUTE sp_executesql @sql
  26.  
  27. -- Reset identity counter if one exists
  28. IF ((SELECT OBJECTPROPERTY( OBJECT_ID(@catalog + '.' + @schema + '.' + @tbl), 'TableHasIdentity')) = 1)
  29. BEGIN
  30. SET @sql = N'DBCC CHECKIDENT ([' + @catalog + '.' + @schema + '.' + @tbl + '], RESEED, 0)'
  31. PRINT 'Executing statement: ' + @sql
  32. EXECUTE sp_executesql @sql
  33. END
  34.  
  35. FETCH NEXT FROM i INTO @catalog, @schema, @tbl;
  36. END
  37. CLOSE i;
  38. DEALLOCATE i;
  39.  
  40. -- Enable all constraints for database
  41. EXEC sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement