如何通过存储过程在表中逐字符搜索 [英] How to search character by character in a table through stored procedure

查看:106
本文介绍了如何通过存储过程在表中逐字符搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在表格中搜索字符或单词.例如,我要执行以下步骤...

1)加载所有记录

2)查看所有记录并检查匹配字符数

3)返回那些具有更多匹配字符的记录.


如何通过存储过程做到这一点?


关于

i want to search character OR word in a table.EG i want to do the following steps...

1)load all records

2)Loop all records and check number of matching characters

3)Return those records have more matched characters.


how i can do this through Stored procedure?


Regards

推荐答案

一种方法是:
-您可以使用 DECLARE CURSOR [ FETCH NEXT [ CHARINDEX [临时表 [
One way is that:
- you define a cursor for your select using DECLARE CURSOR[^]
- loop through the records using FETCH NEXT[^]
- in the loop use CHARINDEX[^] to find the number of occurences

If you want to return several rows, you can create a temporary table[^] and add rows to return into that table and in the end select from that table in order to return the results.

However, if you''re only interested in rows having most of certain character, you don''t need a procedure for that. You can count the number of occurences by comparing the original length of a string to a length of a string where desired characters are removed. Consider the following:
select SomeColumn, 
       len(SomeColumn) - len(replace(SomeColumn, 'SomeCharacter', '')) as numberofoccurences 
from sometable


这篇关于如何通过存储过程在表中逐字符搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆