Wednesday, April 1, 2009

sp_MSforeachtable -Nice feature of sql server

  • sp_MSforeachtable is a predefined stored procedure used to perform common action in all table of database at same time.
  • For Example:Traditionally if you wanted to run a DBCC CHECKTABLE on every table in a database you'd have to write an elaborate cursor with couple of lines of sql script.
  • Using the question mark as a place holder for all table names, the procedure will do the common task in a single line query which actually takes multiple lines in traditional way.
  • Example Code : sp_MSforeachtable @command1="print '?' dbcc checktable ('?')"
  • You can provide up to 3 commands to the stored procedure using @command1 through @command3
  • enjoy the fun programming...

No comments: