简体   繁体   English

如何选择任何列具有特定值的sql数据库表中的所有记录

[英]How to select all records in sql database tables where any column has a particular value

I'm trying to find all the tables in my database that contain a particular email address. 我正在尝试查找数据库中包含特定电子邮件地址的所有表。

The only thing I know is that this means that I'm looking for any column that is a varchar of some length. 我唯一知道的是,这意味着我正在寻找长度一定的varchar的任何列。

I was thinking about some sort of loop through sys.tables and then for each table in that loop, a loop through the rows in the table and then evaluation of each column on each row. 我正在考虑通过sys.tables某种循环,然后针对该循环中的每个表,对表中的行进行循环,然后对每一行中的每一列进行求值。

Probably not the best way to go about it but there's things I don't know, particularly: 可能不是最好的解决方法,但是有些事情我还是不知道,尤其是:

  1. How better to do this, and 如何更好地做到这一点,以及
  2. How to write this query in the first place. 首先如何编写此查询。

Any assistance will be greatly appreciated. 任何帮助将不胜感激。

You could write a quick query that would build queries for each field. 您可以编写一个快速查询,为每个字段建立查询。

select 'select * from ' + TABLE_NAME + ' where ' + COLUMN_NAME + ' = ''the@email.com''' 
from INFORMATION_SCHEMA.COLUMNS
where DATA_TYPE = 'nvarchar' and CHARACTER_MAXIMUM_LENGTH = '256'

You're probably looking at something like sp_MSforeachtable. 您可能正在查看类似sp_MSforeachtable的内容。 Find all tables with columns with [varchar] types ([nvarchar] as well right? Could they be on [char] or [nchar] columns as well?) from [sys].[columns]. 在[sys]。[columns]中找到所有具有[varchar]类型的列的表([nvarchar]也正确吗?它们是否也可以在[char]或[nchar]列上?)。 Then sp_MSforeachtable to access the values in the columns. 然后,sp_MSforeachtable访问列中的值。

Basically you're looking at nested cursors. 基本上,您正在看嵌套游标。 One to get all "text" columns along with the associated tables. 一种用于获取所有“文本”列以及关联的表。 Another to run a select on each table and column. 另一个在每个表和列上运行选择。

This is brute force stuff and is not pretty. 这是蛮力的东西,并不漂亮。

I've been in environments with an existing (and often ancient and revered) design as well. 我也曾在一个已有(通常是古老且受人尊敬的)设计的环境中工作。 Sometimes you just have to do stuff like this. 有时您只需要做这样的事情。 I like writing SQL so it can be fun. 我喜欢编写SQL,因此很有趣。

It is always nice to fantasize about building stuff from scratch to some ideal of perfection, but no plan is perfect. 幻想从头开始构建东西到一些完美的理想总是很好,但是没有一个计划是完美的。 You have to be able to do stuff like this. 您必须能够做这样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL:如何查找列中所有记录具有相同值的值总和? - SQL: How to find the sum of values where all the records has the same value in a column? 如何从表 A 中返回所有记录,如果任何一列在 oracle sql 中具有特定值? - How to return all records from table A , if any one of the column has a specific value in oracle sql? 如何选择所有结果中列值相同的记录集 - How to select a set of records where a column value is the same in all results SQL:如何从 2 个表中选择所有记录 - SQL: How to select all records from 2 tables select 列具有特定值的所有表中的表名 - select table name from all tables where column has specific value 如果列只有两个特定值,则sql select all - sql select all if column has only two particular values 如何选择字段具有特定值的所有记录,直到显示具有不同值的记录? - How to select all records where a field is of a certain value until a record shows up that has a different value? 选择相关表中没有值的记录的所有行 - Select all rows where related table has no records with value SQL Select条目,其中所有实体在特定列中都不具有值 - SQL Select entries where none of the entities have a value in a particular column SQL Server:选择所有行,如果4列中的任何一列的值为“ N” - SQL Server: Select all rows where if any one of the 4 columns has a value of 'N'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM