Usage of sp_msforeachtable and sp_msforeachdb in MSSQL

Source: Internet
Author: User

SQL queries the list of all user tables, such as the number of records and the occupied table size.

Exec Sp_msforeachtable" Execute Sp_spaceused ' ? ' "

Usage of sp_msforeachtable and sp_msforeachdb in MSSQL
Since mssql6.5, Microsoft has provided two very useful system stored procedures: sp_msforeachtable and sp_msforeachdb, which are used to traverse each table of a database and traverse each database under DBMS management.

Usage of sp_msforeachtable and sp_msforeachdb in MSSQL

Execute the following statement in the master database to view the detailedCode

Use Master

Exec Sp_helptext sp_msforeachtable

Exec Sp_helptext sp_msforeachdb

The sp_msforeachtable system stored procedure has seven parameters, which are explained as follows:

@ Command1 Nvarchar ( 2000 ), -- The first T-SQL command to run

@ Replacechar Nchar ( 1 ) = N ' ? ' , -- The specified placeholder.

@ Command2 Nvarchar ( 2000 ) = Null , -- Second run T-SQL command

@ Command3 Nvarchar ( 2000 ) = Null , -- Article 3 run T-SQL instructions

@ Whereand Nvarchar ( 2000 ) = Null , -- (Optional) Select a table.

@ Precommand Nvarchar ( 2000 ) = Null , -- Commands executed before the table

@ Postcommand Nvarchar ( 2000 ) = Null -- Command executed after the table

Usage of sp_msforeachtable and sp_msforeachdb in MSSQL

-- Besides @ whereand, sp_msforeachdb is the same as sp_msforeachtable.

-- Let's take a look at the usage of sp_msforeachtable (the usage of sp_msforeachdb is similar ):

-- Details of each table in the statistics database:

Exec Sp_msforeachtable @ Command1 = "Sp_spaceused ' ? ' "

-- Queries the total number of records of all tables in the database.
Create Table # Temp (Tablename Varchar ( 255 ), Rowcnt Int )
Exec Sp_msforeachtable ' Insert into # temp select '' ? '' , Count (*) from? '
Select Tablename, rowcnt From # Temp Order By Tablename
Drop Table # Temp
 
-- Check the integrity of data, indexes, text, ntext, and image pages of each table or index view in the database.

-- The following statement must be executed in single-user mode (sp_dboption 'db _ name', 'single user', 'true'). If it is set to true, it becomes a multi-user.

Exec Sp_msforeachtable" DBCC Checktable ( ' ? ' , Repair_rebuild )"

SQL queries the list of all user tables, such as the number of records and the occupied table size.

Exec Sp_msforeachtable" Execute Sp_spaceused ' ? ' "

Usage of sp_msforeachtable and sp_msforeachdb in MSSQL
Since mssql6.5, Microsoft has provided two very useful system stored procedures: sp_msforeachtable and sp_msforeachdb, which are used to traverse each table of a database and traverse each database under DBMS management.

Usage of sp_msforeachtable and sp_msforeachdb in MSSQL

Run the following statement in the master database to view the detailed code of two proc instances.

Use Master

Exec Sp_helptext sp_msforeachtable

Exec Sp_helptext sp_msforeachdb

The sp_msforeachtable system stored procedure has seven parameters, which are explained as follows:

@ Command1 Nvarchar ( 2000 ), -- The first T-SQL command to run

@ Replacechar Nchar ( 1 ) = N ' ? ' , -- The specified placeholder.

@ Command2 Nvarchar ( 2000 ) = Null , -- Second run T-SQL command

@ Command3 Nvarchar ( 2000 ) = Null , -- Article 3 run T-SQL instructions

@ Whereand Nvarchar ( 2000 ) = Null , -- (Optional) Select a table.

@ Precommand Nvarchar ( 2000 ) = Null , -- Commands executed before the table

@ Postcommand Nvarchar ( 2000 ) = Null -- Command executed after the table

Usage of sp_msforeachtable and sp_msforeachdb in MSSQL

-- Besides @ whereand, sp_msforeachdb is the same as sp_msforeachtable.

-- Let's take a look at the usage of sp_msforeachtable (the usage of sp_msforeachdb is similar ):

-- Details of each table in the statistics database:

Exec Sp_msforeachtable @ Command1 = "Sp_spaceused ' ? ' "

-- Queries the total number of records of all tables in the database.
Create Table # Temp (Tablename Varchar ( 255 ), Rowcnt Int )
Exec Sp_msforeachtable ' Insert into # temp select '' ? '' , Count (*) from? '
Select Tablename, rowcnt From # Temp Order By Tablename
Drop Table # Temp
 
-- Check the integrity of data, indexes, text, ntext, and image pages of each table or index view in the database.

-- The following statement must be executed in single-user mode (sp_dboption 'db _ name', 'single user', 'true'). If it is set to true, it becomes a multi-user.

Exec Sp_msforeachtable" DBCC Checktable ( ' ? ' , Repair_rebuild )"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.