65.9K
CodeProject is changing. Read more.
Home

Loop through all tables in SQL Server

starIconstarIconstarIconstarIconstarIcon

5.00/5 (4 votes)

Feb 24, 2011

CPOL
viewsIcon

28832

Use this when you want to perform a task on all tables in a database

sp_MSforeachtable is a stored procedure used to loop through all the tables in the database. We can use this in different situations. For example, we want to run command to disable all triggers, then we could write that like this:
sp_msforeachtable "ALTER TABLE ? DISABLE TRIGGER all"
? will be replaced with table name. I think you will enjoy this. For details, click here[^]