<i id='wdkAJ'><tr id='wdkAJ'><dt id='wdkAJ'><q id='wdkAJ'><span id='wdkAJ'><b id='wdkAJ'><form id='wdkAJ'><ins id='wdkAJ'></ins><ul id='wdkAJ'></ul><sub id='wdkAJ'></sub></form><legend id='wdkAJ'></legend><bdo id='wdkAJ'><pre id='wdkAJ'><center id='wdkAJ'></center></pre></bdo></b><th id='wdkAJ'></th></span></q></dt></tr></i><div id='wdkAJ'><tfoot id='wdkAJ'></tfoot><dl id='wdkAJ'><fieldset id='wdkAJ'></fieldset></dl></div>

      <small id='wdkAJ'></small><noframes id='wdkAJ'>

    1. <legend id='wdkAJ'><style id='wdkAJ'><dir id='wdkAJ'><q id='wdkAJ'></q></dir></style></legend>
        <bdo id='wdkAJ'></bdo><ul id='wdkAJ'></ul>

      1. <tfoot id='wdkAJ'></tfoot>

        如何对一个实例上的所有数据库运行相同的查询?

        时间:2023-07-17
          1. <small id='hmgSq'></small><noframes id='hmgSq'>

                <tfoot id='hmgSq'></tfoot>
                <i id='hmgSq'><tr id='hmgSq'><dt id='hmgSq'><q id='hmgSq'><span id='hmgSq'><b id='hmgSq'><form id='hmgSq'><ins id='hmgSq'></ins><ul id='hmgSq'></ul><sub id='hmgSq'></sub></form><legend id='hmgSq'></legend><bdo id='hmgSq'><pre id='hmgSq'><center id='hmgSq'></center></pre></bdo></b><th id='hmgSq'></th></span></q></dt></tr></i><div id='hmgSq'><tfoot id='hmgSq'></tfoot><dl id='hmgSq'><fieldset id='hmgSq'></fieldset></dl></div>
                  <tbody id='hmgSq'></tbody>
                  <bdo id='hmgSq'></bdo><ul id='hmgSq'></ul>
                  <legend id='hmgSq'><style id='hmgSq'><dir id='hmgSq'><q id='hmgSq'></q></dir></style></legend>

                  本文介绍了如何对一个实例上的所有数据库运行相同的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我(出于测试目的)在 sql server 2008 r2 实例上有许多具有相同架构(=基本上相同的表和列)的数据库.

                  I have (for testing purposes) many dbs with the same schema (=same tables and columns basically) on a sql server 2008 r2 instance.

                  我想要一个类似的查询

                  SELECT COUNT(*) FROM CUSTOMERS
                  

                  在实例上的所有数据库上.我想要结果为 2 列:

                  on all DBs on the instance. I would like to have as result 2 columns:

                  1 - 数据库名称

                  2 - COUNT(*)

                  示例:

                  DBName  //   COUNT (*)
                  
                  TestDB1 // 4
                  
                  MyDB  // 5
                  
                  etc...
                  

                  注意:我假设所有数据库中都存在 CUSTOMERS 表(master 除外).

                  Note: i assume that CUSTOMERS table exists in all dbs (except master).

                  推荐答案

                  试试这个 -

                      SET NOCOUNT ON;
                      
                      IF OBJECT_ID (N'tempdb.dbo.#temp') IS NOT NULL
                         DROP TABLE #temp
                      
                      CREATE TABLE #temp
                      (
                            [COUNT] INT
                          , DB VARCHAR(50)
                      )
                      
                      DECLARE @TableName NVARCHAR(50) 
                      SELECT @TableName = '[dbo].[CUSTOMERS]'
                      
                      DECLARE @SQL NVARCHAR(MAX)
                      SELECT @SQL = STUFF((
                          SELECT CHAR(13) + 'SELECT ' + QUOTENAME(name, '''') + ', COUNT(1) FROM ' + QUOTENAME(name) + '.' + QUOTENAME(@TableName)
                          FROM sys.databases 
                          WHERE OBJECT_ID(QUOTENAME(name) + '.' + QUOTENAME(@TableName)) IS NOT NULL
                          FOR XML PATH(''), TYPE).value('text()[1]', 'NVARCHAR(MAX)'), 1, 1, '')
                      
                      INSERT INTO #temp (DB, [COUNT])              
                      EXEC sys.sp_executesql @SQL
                      
                      SELECT * 
                      FROM #temp t
                  

                  输出(例如,在 AdventureWorks 中)-

                  Output (for example, in AdventureWorks) -

                  COUNT       DB
                  ----------- --------------------------------------------------
                  19972       AdventureWorks2008R2
                  19975       AdventureWorks2012
                  19472       AdventureWorks2008R2_Live
                  

                  这篇关于如何对一个实例上的所有数据库运行相同的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:T-SQL:四舍五入到最接近的 15 分钟间隔 下一篇:SQL 查询以逗号分隔符以及 SQL Server 中的按列分组获取聚合结果

                  相关文章

                • <small id='Tp4zZ'></small><noframes id='Tp4zZ'>

                  <legend id='Tp4zZ'><style id='Tp4zZ'><dir id='Tp4zZ'><q id='Tp4zZ'></q></dir></style></legend>
                  • <bdo id='Tp4zZ'></bdo><ul id='Tp4zZ'></ul>

                      <i id='Tp4zZ'><tr id='Tp4zZ'><dt id='Tp4zZ'><q id='Tp4zZ'><span id='Tp4zZ'><b id='Tp4zZ'><form id='Tp4zZ'><ins id='Tp4zZ'></ins><ul id='Tp4zZ'></ul><sub id='Tp4zZ'></sub></form><legend id='Tp4zZ'></legend><bdo id='Tp4zZ'><pre id='Tp4zZ'><center id='Tp4zZ'></center></pre></bdo></b><th id='Tp4zZ'></th></span></q></dt></tr></i><div id='Tp4zZ'><tfoot id='Tp4zZ'></tfoot><dl id='Tp4zZ'><fieldset id='Tp4zZ'></fieldset></dl></div>
                      <tfoot id='Tp4zZ'></tfoot>