I'm trying to do query on table where a field's text is contained in a variable.
Example:
Table A
ID Ref Processed
----|-----------------|---------
1 | I want a pool | 0
2 | I want a bike | 0
Table B
ID Text
----|---------------
1 | pool
2 | bike
Get all unprocessed rows from TableA
into #tempTable
select * into #tmpTable from TableA where processed = 0
I then cycle over each row in #tmpTable
and set @Variable
to the current field Ref
.
So the first time through @Variable = 'I want a pool'
and then @Variable = 'I want a bike'
.
I then need to query TableB
where the Text
field is contained inside @Variable
and select the row.
I know the following doesn't work, but it's where I need to end up:
select * from TableB where @Variable LIKE '% '+Text+' %'
I'm editing an existing stored procedure so if there's a better way to go about this then I'm open to all suggestions.
Is this what you are looking for?
select a.*, b.text
from tableA a join
tableB b
on a.processed = 0 and
ref like '%'+b.text+'%';
EDIT:
If you want to match words that are separated by spaces:
on a.processed = 0 and
' '+ref+' ' like '% '+b.text+' %';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.