Search all databases for value

后端 未结 4 1109
广开言路
广开言路 2020-12-16 08:22

Consider have a table like named : People

| Id    | Name    | Code   |
| 1     | John    | 857    |
| 2     | Mike    | 893    |
| 3     | Sara          


        
4条回答
  •  猫巷女王i
    2020-12-16 09:14

    This seems like a crazy thing to do - the search could completely kill your server. That said, if this is a one-off/occasional use manual run kind of thing then you can create a script to generate the SQL commands you need and then execute the result sets. I've done this sort of thing before to refactor columns in a database. Your script would need to first run a query to get the names of all the DBs on your sql-server instance

      SELECT * FROM sys.databases
    

    Then create dynamic sql including the USE command to specify the DB and use the information_schema.tables and .columns to get the names of all the tables and columns you want to search - up to you how you decided to select the columns to be searched (is it just columns like N'%name%' for example.

    Then use this information in a for loop to create individual table/column searches. For efficiency (and to help you debug if it's not working) I suggest utilise @table variables.

    Finally select the contents of any @tables holding the table queries you've created.

    This output can then be run on the server - providing you've included the use commands or prefixed table names with [dbname].[schema].

提交回复
热议问题