如何在整个数据库中查找字符串?
我有一个特定的字符串,例如“123abcd”,但我不知道表的名称,甚至不知道 SQL Server 数据库上表内的列的名称。我想用 select 找到它并显示相关字符串的所有列,所以我想知道类似的事情:
select * from Database.dbo.* where * like '%123abcd%'
出于明显的原因它不起作用,但是有一种简单的方法可以创建一个 select 语句来执行类似的操作?
I have one specific string, such as "123abcd" for example but I don't know the name of the table or even the name of the column inside the table on my SQL Server Database. I want to find it with a select and show all columns of the related string, so I was wondering something like:
select * from Database.dbo.* where * like '%123abcd%'
For obvious reasons it doens't work, but there is a simple way to create a select statement to do something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这会起作用:
不过,有一些警告。首先,这非常慢且未经优化。所有值都被简单地转换为
nvarchar
,以便可以毫无错误地进行比较。您可能会遇到诸如datetime
之类的值未按预期转换并因此在应匹配时未匹配的问题(漏报)。WHERE (0 = 1)
的作用是使构建OR
子句变得更加容易。如果没有匹配,您将不会得到任何行。This will work:
A couple caveats, though. First, this is outrageously slow and non-optimized. All values are being converted to
nvarchar
simply so that they can be compared without error. You may run into problems with values likedatetime
not converting as expected and therefore not being matched when they should be (false negatives).The
WHERE (0 = 1)
is there to make building theOR
clause easier. If there are not matches you won't get any rows back.这里有更多可用于此目的的免费工具。两者都作为 SSMS 插件工作。
ApexSQL 搜索 – 100% 免费 - 搜索表中的架构和数据。有几个更有用的选项,例如依赖项跟踪...
SSMS 工具包 – 对于除 SQL 2012 之外的所有版本免费 – 没有它看起来不像以前的那么先进,但还有很多其他很酷的功能。
Here are couple more free tools that can be used for this. Both work as SSMS addins.
ApexSQL Search – 100% free - searches both schema and data in tables. Has couple more useful options such as dependency tracking…
SSMS Tools pack – free for all versions except SQL 2012 – doesn’t look as advanced as previous one but has a lot of other cool features.
我认为你有两个选择:
使用
sys.tables
和sys.columns
构建动态SQL来执行搜索(示例)。使用任何具有此功能的程序。例如 SQL Workbench(免费)。
I think you have two options:
Build a dynamic SQL using
sys.tables
andsys.columns
to perform the search (example).Use any program that have this function. An example of this is SQL Workbench (free).
在oracle中你可以使用如下sql命令来生成你需要的sql命令:
In oracle you can use the following sql command to generate the sql commands you need:
通用资源 Grep (crgrep) 将按名称或内容在表/列中搜索字符串匹配,并支持多种数据库,包括 SQLServer、Oracle 等。完整的通配符和其他有用的选项。
它是开源的(我是作者)。
http://sourceforge.net/projects/crgrep/
Common Resource Grep (crgrep) will search for string matches in tables/columns by name or content and supports a number of DBs, including SQLServer, Oracle and others. Full wild-carding and other useful options.
It's opensource (I'm the author).
http://sourceforge.net/projects/crgrep/
我通常使用
information_Schema.columns
和information_schema.tables
,尽管像 @yuck 所说,sys.tables
和sys.columns
code> 的输入时间较短。在循环中,连接这些
然后执行生成的 sql。
I usually use
information_Schema.columns
andinformation_schema.tables
, although like @yuck said,sys.tables
andsys.columns
are shorter to type.In a loop, concatenate these
Then execute the resulting sql.
这是一个简单方便的基于光标的解决方案
Here is an easy and convenient cursor based solution
这是 @Raihan 的 PostgreSQL 版本答案,位于 https://stackoverflow.com/a/8436114/5869805 。它还利用了 https://stackoverflow.com/a/58319308/5869805 上的答案。
Here is the PostgreSQL version of @Raihan's answer at https://stackoverflow.com/a/8436114/5869805 . It also makes use of the answer at https://stackoverflow.com/a/58319308/5869805 .