【问题标题】:How to search a table in all databases where column value is the given text如何在列值为给定文本的所有数据库中搜索表
【发布时间】:2017-09-05 17:45:18
【问题描述】:

我可以通过How to run the same query on all the databases on an instance? 查询所有数据库,但不确定如何通过匹配特定列和值来运行查询。

例如,假设列名是代码。查询 + 代码='THBN'

谢谢。

【问题讨论】:

    标签: sql-server


    【解决方案1】:
    EXECUTE sp_MSForEachDB 
    'USE ?; 
    SELECT * from table where code=''tbhn'''; 
    

    你也可以像下面这样忽略系统数据库

    EXECUTE sp_MSForEachDB 
    'if db_id()>4
    begin
    SELECT * from table where code=''tbhn''
    end'; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-17
      • 2014-09-23
      • 1970-01-01
      • 2015-03-11
      • 2022-06-10
      • 2015-07-29
      • 2018-12-23
      相关资源
      最近更新 更多