• -- I find it more useful

    -- to put Sql Browser in text mode...

    -- copy code out of window

    -- and run it.

    --

    -- I did this by allowing it to continue after it finds an error...(added a go)

    -- I also added '--' to the Column header so that it will be ignored when the code is run.

    --

    --Generates repair statements for not-trusted foreign keys

    sp_MSforeachdb'

    IF EXISTS(SELECT 1 FROM [?].sys.foreign_keys WHERE is_not_trusted = 1)

    BEGIN

    SELECT

    ''go

    ALTER TABLE [?].[''+s.name+''].[''+o.name+''] WITH CHECK

    CHECK CONSTRAINT [''+fk.name+'']'' AS [--CheckForeignKeyCommand]

    FROM [?].sys.foreign_keys fk

    INNER JOIN [?].sys.objects o ON fk.parent_object_id = o.object_id

    AND fk.is_not_trusted = 1

    AND fk.is_not_for_replication = 0

    INNER JOIN [?].sys.schemas s ON o.schema_id = s.schema_id

    END'