Hi. I have a table
Which contains about 100 items I need to search in 75 databases. I can run something like this:
And I receive results just fine.
Instead of running 75 different queries, I have created a table and inserted all 75 database names in it:
I am trying a 'while' loop to search each file in the 'searchlist' by joining it with each of my 75 databases:
I am getting something wrong in the @dynamicSQL syntax. I don't think I am joining the tables correctly as far as the syntax goes.
Can anyone tell me what I'm missing here?
Thanks!
Code:
create table searchlist (list varchar (100))
Which contains about 100 items I need to search in 75 databases. I can run something like this:
Code:
select t1.list from MyDB1..files t1
join searchlist t2
on t1.list like '%' + t2.list + '%'
And I receive results just fine.
Instead of running 75 different queries, I have created a table and inserted all 75 database names in it:
Code:
create table dblist (counter int identity(1,1), list varchar(100), processed varchar (1))
insert into dblist (list) values ('MyDB1')
insert into dblist (list) values ('MyDB2')
insert into dblist (list) values ('MyDB3')
insert into dblist (list) values ('MyDB4')
etc.
I am trying a 'while' loop to search each file in the 'searchlist' by joining it with each of my 75 databases:
Code:
while (select count(counter) from dblist where processed is null) > 0
begin
declare @mindb varchar(100)
declare @dynamicsql nvarchar(2000)
set @mindb = (select min(list) from dblist where processed is null)
set @dynamicsql = 'insert into results
select t1.list from ' + @mindb + '..files t1 ' +
'join searchlist
on t1.list like ' + '%' + '''' + ' + searchlist.list + ' + '''' + '%' + ''''
print @dynamicsql
sp_executesql @dynamicsql
update sharelist set processed = 'x'
where list = @mindb
end
I am getting something wrong in the @dynamicSQL syntax. I don't think I am joining the tables correctly as far as the syntax goes.
Can anyone tell me what I'm missing here?
Thanks!