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

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.

Submit to Hacker News

[NOTE] This post has been imported to Hugo from a Wordpress blog. It may be broken, missing images, code snippets or other information.

[NOTE] updated January 16, 2010. It's 3299 days old . This article may have outdated content.