Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to run same query on multiple databases 1

Status
Not open for further replies.

angiem

MIS
Sep 29, 2000
116
0
0
CA
I am trying to count the records on the same table but on different databases then I want to put it in another table and read the results from there. The code I have at the moment is this

declare @cmd1 varchar(500)
set @cmd1 =
'if ''?'' like ''abcd%'' Select ''?'', count(*)
From tablename
exec sp_MSforeachdb @command1=@cmd1

the problem is that it is producing the same results for all the databases which I know is wrong. Can anyone help or is there a better way.

Angie
 
I now have to do it across different servers too and changed the code it now ignores the 'if' statement and gives the error message

OLE DB provider 'servername' does not contain table '"master"."tablename"'

declare @cmd1 varchar(500)
set @cmd1 =
'if ''?'' like ''abcd%'' Select ''?'', count(*)
From [servername].[?]..tablename'
exec sp_MSforeachdb @command1=@cmd1

thanks

Angie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top