Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
/ sp_SqlForeachDB Public archive

Executes a script against each database in an instance. The goal of this procedure is to improve upon the undocumented system proc sp_MSforeachdb, by providing: documentation, options, messaging, and error handling.

License

Notifications You must be signed in to change notification settings

2Toad/sp_SqlForeachDB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Archived Repository 🚨

This project is no longer maintained and has been archived. No further issues, PRs, or updates will be made.


#sp_SqlForeachDB

Executes a script against each database in an instance. The goal of this procedure is to improve upon the undocumented system proc sp_MSforeachdb, by providing: documentation, options, messaging, and error handling.

##Example

This example runs multiple scripts against all the user databases that are not read-only; stops execution of the script against the remaining databases if an error occurs; returns 0 if there is an error, so execution of the next script can be stopped.

DECLARE @sql NVARCHAR(MAX)
        , @scriptSuccess BIT;

SET @sql = N'
        USE {db};
        IF EXISTS (SELECT * FROM sys.schemas WHERE name = ''foo'')
                PRINT ''Schema already exists in {db}'';
        ELSE 
        BEGIN
                PRINT ''Adding schema to {db}'';
                EXEC spCreateSchema @schema  = ''foo'', @purpose = ''bar.''
        END';

EXEC @scriptSuccess = sp_SqlForeachDB
        @script = @sql
        ,@excludeSystem = 1
        ,@excludeReadOnly = 1;

IF @scriptSuccess = 0
BEGIN
        PRINT 'Execution aborted! Please view the message log for more details.';
        RETURN;
END

-- Next script
SET @sql = N'   USE {db};...

About

Executes a script against each database in an instance. The goal of this procedure is to improve upon the undocumented system proc sp_MSforeachdb, by providing: documentation, options, messaging, and error handling.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages