Execute a T-Sql statement on all Databases – Sql Server

Posted on Jan 16, 2010

Sometimes, you may want to execute a single statement (t-sql) on all of your databases.

I had this issue about a week ago, I needed to change my users table on all databases. Because all of my tables are named the same on all db’s (convention) I wanted to avoid the hassle of going through the databases manually with Management studio and just wanted to execute the code on all of the db’s.

This is the script.

I added some comments to better understand it.