本文介紹了從 csv 字符串進行 sql 搜索的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在做一個搜索頁面,我必須用一個文本框搜索多個字段.所以我會在我的存儲過程中以 CSV 字符串的形式獲取搜索文本
im doing a search page where i have to search multiple fields with a single textbox. so i will get the search text as a CSV string in my stored procedure
我的表格如下
ID Name age
5 bob 23
6 bod.harry 34
7 charles 44
我需要一個類似這樣的 sql 查詢
i need a sql query something like this
declare @searchtext='bob,harry,charley'
select * from employee where name like (@searchtext)
此查詢應返回此記錄(id 5 和 6)
this query should return both this records (id 5 and 6)
推薦答案
你可以在存儲過程中使用這種方式,
You can use this way in Stored Procedure,
declare @searchtext varchar(1000)
set searchtext ='bob,harry,charley'
declare @filter varchar(2000)
set @filter = '(name LIKE ''%' + replace('bob,harry,charley',',','%'' OR name LIKE ''%') + '%'')'
exec
('
select *
from mytab
where ' + @filter + '
'
)
這篇關于從 csv 字符串進行 sql 搜索的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!