如何使用循环进行类似或Ilike功能?
目前,我被要求验证全局搜索的结果,该结果以查找包含某些关键字的所有记录交叉整个数据库。为此,我需要检查每个具有关键字的表中的所有行。此全局搜索的结果已准备就绪,部分结果看起来像下面的结果:
table_name | column_name | 关键字 | cnt |
---|---|---|---|
wf_process | name_en | fec | 11 |
wf_process | ftablename | gb | 14 |
iccclass | name_en | gb | 4 |
我试图使用的是使用“喜欢”操作员提取'每个表中包含关键字的列中的所有数据。也就是说,我将使用以下查询:
select distinct Name_EN, FTABLENAME from Wf_Process where Name_EN like '%FEC%' or FTABLENAME like '%GB%'
并
select distinct Name_EN from ICCClass where Name_EN like '%GB%'
删除整个数据库中所需的所有数据。如果我只有三个记录,那不是问题。但是,我的结果返回了数千个表格,总共包含10K行。因此,我试图使用循环来执行此任务,但我失败了。
因此,我的问题是,有人有任何想法写一个循环来一次搜索所有表吗?还是有其他方式而不是循环可以在SQL Server中执行此操作?
非常感谢!
I am currently asked to validate the result of a global search which to find all records containing some keywords cross whole database. To do so, I need to check all the rows in each table that have keywords. The result for this global search is ready and partially of the result looks like the one below:
table_name | column_name | keyword | cnt |
---|---|---|---|
Wf_Process | Name_EN | FEC | 11 |
Wf_Process | FTABLENAME | GB | 14 |
ICCClass | Name_EN | GB | 4 |
What I am trying to do is using the 'like' operator to extract all the data in each table where columns containing keywords. That is, I will use query such as:
select distinct Name_EN, FTABLENAME from Wf_Process where Name_EN like '%FEC%' or FTABLENAME like '%GB%'
and
select distinct Name_EN from ICCClass where Name_EN like '%GB%'
to pull out all data I need in whole database. If I only have three records, then it is not a problem. However my result returned over thousands tables have more than 10K rows containing keywords in total. Therefore, I was trying to use a loop to do this mission but I failed.
My question therefore is, does anyone have any idea to write a loop to do the 'like' search for all the tables in one time? Or is there another way rather than loop can do this in SQL Server?
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要动态SQL。您可以一起生成一个大的
联合所有
查询所有表格。如果每个表有许多列或关键字,则有更有效的方法可以做到这一点,但这应该使您开始。
You need dynamic SQL for this. You can generate a big
UNION ALL
query of all the tables together.There are more efficient ways to do this if there are many columns or keywords per table, but this should get you started.
这个问题看起来很奇怪,您可以通过动态SQL完成,但不像一个常规解决方案更像一个或POC/POC/测试。
The question looks odd, thou can be done via dynamic sql, but not as a regular solution more like a one off or POC/test.