sp_MSforeachtable简介; 遍历数据库中的所有表运行命令

介绍 (Introduction)

There have always been some undocumented objects in SQL Server that are used internally by Microsoft, but they can be used by anybody that have access to it. One of those objects is a stored procedure called sp_MSforeachtable.

Microsoft内部始终使用SQL Server中一些未记录的对象,但是有权访问它的任何人都可以使用它们。 这些对象之一是称为sp_MSforeachtable的存储过程。

sp_MSforeachtable is a stored procedure that is mostly used to apply a T-SQL command to every table, iteratively, that exists in the current database.

sp_MSforeachtable是一个存储过程,通常用于将T-SQL命令迭代地应用于当前数据库中存在的每个表。

Because official documentation from Microsoft doesn’t exist, the majority of people do not know about the existence of all of these parameters and how this stored procedure can be used to perform more than a simple operation over all the database tables.

由于不存在Microsoft的官方文档,因此大多数人不知道所有这些参数的存在以及如何使用此存储过程对所有数据库表执行简单的操作以外的其他操作。

This is main purpose of this article. To provide as much information as possible for this undocumented SQL Server stored procedure so everybody can take the maximum advantage when use it.

这是本文的主要目的。 为该未记录SQL Server存储过程提供尽可能多的信息,以便每个人都可以在使用它时最大程度地利用它。

典型用法 (Typical usages)

From my experience, I have found in the majority of use cases, people using this stored procedure to:

根据我的经验,我发现在大多数用例中,使用此存储过程的人员可以:

  • perform an unconditional reindex over all tables in the database:

    对数据库中的所有表执行无条件重新索引:
 
  EXEC sp_MSforeachtable 'DBCC DBREINDEX(''?'')'
 
  • truncate all tables in the database:

    截断数据库中的所有表:
 
  EXEC sp_MSforeachtable 'TRUNCATE TABLE ?'
 
  • get the information about the number of records from all tables in the database:

    从数据库的所有表中获取有关记录数的信息:
 
  EXEC sp_MSforeachtable 'SELECT ''?'' TableName, Count(1) NumRecords FROM ?'
 

I think by now you should get the idea on how to use this undocumented stored procedure and also realized that the question mark (?) it is used as the replacement of the table and during the execution it will be replaced by the appropriate table name.

我认为,现在您应该对如何使用此未记录的存储过程有所了解,并且还意识到将问号(?)用作表的替换,并且在执行过程中它将被适当的表名替换。

But the above are only the examples for the typical usage of the sp_MSforeachtable stored procedure. This stored procedure allows more parameters. If only one is passed (as the above examples) it will use by default the @command1 parameter.

但是以上只是sp_MSforeachtable存储过程的典型用法示例。 此存储过程允许更多参数。 如果仅传递一个(如上述示例),则默认情况下将使用@ command1参数。

sp_MSforeachtable参数 (sp_MSforeachtable parameters)

So how to kn

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 SQL Server 提供的系统存储过程 sp_MSforeachtable遍历所有,并使用 SELECT 语句获取每个的数据。以下是一个 C++ 实现的示例代码: ```cpp #include <iostream> #include <sql.h> #include <sqlext.h> #define SQL_RESULT_LEN 240 #define SQL_RETURN_CODE_LEN 1000 using namespace std; int main() { // Declare variables SQLHANDLE sqlenvhandle; SQLHANDLE sqlconnectionhandle; SQLHANDLE sqlstatementhandle; SQLRETURN retcode; SQLCHAR retconstring[SQL_RETURN_CODE_LEN]; // Allocate environment handle retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlenvhandle); // Set the ODBC version environment attribute if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { retcode = SQLSetEnvAttr(sqlenvhandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0); // Allocate connection handle if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { retcode = SQLAllocHandle(SQL_HANDLE_DBC, sqlenvhandle, &sqlconnectionhandle); // Set the connection timeout attribute if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { SQLSetConnectAttr(sqlconnectionhandle, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0); // Connect to SQL Server retcode = SQLConnect(sqlconnectionhandle, (SQLCHAR*)"your_server_name", SQL_NTS, (SQLCHAR*)"your_username", SQL_NTS, (SQLCHAR*)"your_password", SQL_NTS); // Allocate statement handle if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { retcode = SQLAllocHandle(SQL_HANDLE_STMT, sqlconnectionhandle, &sqlstatementhandle); // Loop through all tables and fetch data if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { retcode = SQLExecDirect(sqlstatementhandle, (SQLCHAR*)"EXEC sp_MSforeachtable 'SELECT * FROM ?'", SQL_NTS); // Fetch and print data for each table while (retcode == SQL_SUCCESS) { SQLCHAR table_name[SQL_RESULT_LEN]; SQLINTEGER len; retcode = SQLFetch(sqlstatementhandle); if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { SQLGetData(sqlstatementhandle, 1, SQL_C_CHAR, table_name, SQL_RESULT_LEN, &len); cout << "Data for table " << table_name << ":" << endl; SQLSMALLINT num_cols; SQLNumResultCols(sqlstatementhandle, &num_cols); while (SQLFetch(sqlstatementhandle) != SQL_NO_DATA) { for (int i = 1; i <= num_cols; i++) { SQLCHAR col_name[SQL_RESULT_LEN]; SQLCHAR col_value[SQL_RESULT_LEN]; SQLINTEGER col_len; SQLDescribeCol(sqlstatementhandle, i, col_name, SQL_RESULT_LEN, &len, NULL, NULL, NULL, NULL); SQLGetData(sqlstatementhandle, i, SQL_C_CHAR, col_value, SQL_RESULT_LEN, &col_len); cout << col_name << ": " << col_value << ", "; } cout << endl; } } } } } } } } // Free resources SQLFreeHandle(SQL_HANDLE_STMT, sqlstatementhandle); SQLDisconnect(sqlconnectionhandle); SQLFreeHandle(SQL_HANDLE_DBC, sqlconnectionhandle); SQLFreeHandle(SQL_HANDLE_ENV, sqlenvhandle); return 0; } ``` 请注意替换代码的 "your_server_name"、"your_username" 和 "your_password" 为你的 SQL Server 服务器名称、用户名和密码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值