Adsense_01

Friday, 21 January 2011

Add a new column to every table in a database

This seems quite a painful job when so many start opening a table and add the new column name, datatype and save. But what about you have to do this for 200 tables in a database and say you have 10 databases. Hmmm.... Such a crap work doing manually but SQL Server infamiliar stored procedure comes to rescue at this point.

The below query can be used to add a new column to every single table in a database.

Use [db]
go
exec sp_MsForEachTable 'alter table ? add Load_date datetime null'


Thats it. You are done. Change the database name for the other databases.

So it is not just adding a column to all table, but modifying anything similar for each table in a database. You can change the SQL statement within the single quotes.

Best of luck.

No comments:

Post a Comment