mikevoorisdankellyadventures mikevoorisdankellyadventures - 3 years ago 251
SQL Question

Query to output results using "like"

I might be over-analyzing this but I have 13,000 records stored in a temp table that only has one column.

I'm trying to determine if those records exist in another database/table but there's no key between the two other than the one column.

The query I run has to use LIKE so something like this...

declare @string Varchar(25) = (select top 1 * from accts)
select content from db2..[mc3] where content like '%'@string+'%'

But I have check to see which one's are in there but I don't want to do it manually one at a time.

Is there a way to have it programmatically go through all of my accounts to see which one's are in that database/table?

Answer Source

This may take a while, but you can get the matching ones using:

select a.??
from accts a 
where exists (select 1
              from db2..mc3 
              where mc3.content like '%' + a.?? +'%'

This gets accounts that are in mc3 according to your rule.

I should note: performance will be pretty bad. Better than a cursor but that's not saying much.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download