Using sp_msforeachtable runs the statements sequential. Most likely spreading things out over a few threads will speed up the process a lot. With Service Broker this can be done in SQL Code.
(Below is code written from memory, I have no SQL Server available to test this on. So there might be some bugs in it. I will update whenever I have a Server available.)
First create an activation procedure
CREATE PROC p_enable_constraints
AS
DECLARE @handle UNIQUEIDENTIFIER
, @message sysname
, @sql nvarchar(max)
WHILE 1=1
BEGIN
BEGIN TRAN
WAITFOR ( RECEIVE TOP(1) @handle = conversation_handle,
@message = message_body
FROM ConstraintQueueReceive), TIMEOUT 1000;
IF @@rowcount = 0
begin
rollback
break;
end
set @sql = N'ALTER TABLE ' + quotename(@message) + N' WITH CHECK CONSTRAINT ALL'
exec (@sql)
if @@error <> 0
begin
rollback tran
break
end
COMMIT TRANSACTION
END
RETURN(0)
GO
Set up the Queues and Services to send and receive messages
CREATE QUEUE ConstraintQueueSend
CREATE SERVICE ConstraintServiceSend
CREATE QUEUE ConstraintQueueReceive
WITH STATUS = ON, ACTIVATION (PROCEDURE_NAME = p_enable_constraints
, MAX_QUEUE_READERS = 8
, EXECUTE AS SELF);
CREATE SERVICE [ConstraintServiceReceive] ON QUEUE ConstraintQueueReceive
Now create a procedure to submit a message for each table
CREATE PROC p_submit_enable_constraints_message @object sysname
AS
DECLARE @handle UNIQUEIDENTIFIER;
DECLARE @message sysname;
BEGIN TRANSACTION;
BEGIN DIALOG @handle FROM SERVICE [ConstraintServiceSend]
TO SERVICE [ConstraintServiceReceive]
WITH ENCRYPTION = OFF;
SEND ON CONVERSATION @handle (@object);
COMMIT TRANSACTION;
END CONVERSATION @handle
GO
Finally we can call the procedure to submit this to the queue.
EXEC sp_msforeachtable 'EXEC p_submit_enable_constraints_message N''?'''
Ideally the result of the alter table is captured and sent back.