grady grady - 1 year ago 130
SQL Question

Run script on multiple DBs (SQL Server)?

Let's say I have some update script:

update sometable set somecolumn = 'somevalue' where xyz = 0

Now let's say I have multiple databases, like DB1, DB2, DB3 and so on. How could I run this script on all of them without doing it manually?

Thanks :)

Answer Source

If you wanted all databases, you can use sp_MSforeachdb:

EXEC sp_MSforeachdb @command1="UPDATE ?..sometable SET somecolumn='somevalue' WHERE xyz=0"

Or for specific databases, you could try some of the logic as seen here:

Hope that helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download