Uncategorized

Reset IDENTITY_INSERT to OFF for all tables

DECLARE @Stmt NVARCHAR(MAX)

SET @Stmt = CAST(
				(SELECT
					'SET IDENTITY_INSERT ' + 
					QUOTENAME(OBJECT_SCHEMA_NAME([t].[object_id])) + '.' + 
					QUOTENAME([t].[name]) + ' OFF' + CHAR(10)
             FROM [sys].[columns] [c] 
				JOIN [sys].[tables] [t] ON [t].[object_id] = [c].[object_id]
             WHERE [c].[is_identity] = 1 
             ORDER BY 1 FOR XML PATH('')) AS NVARCHAR(MAX)
			)

PRINT @Stmt
GO