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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run One Query on all Databases on Server

Status
Not open for further replies.

yeagly21

Programmer
Dec 11, 2003
23
US
I am tyring to figure out how I can run the same query against all databases on the server at one time. I have over 100 databases on this server and I figure there has to be an easy way to run this simple query on all of the databases.

For simplicity we will say the query is:

SELECT Job_ID
FROM Jobs
WHERE PostDate >= '6/8/2006'

If anyone can help I would appreciate it.
 
Code:
exec dbo.sp_MSforeachdb 'IF NOT OBJECT_ID(''?.dbo.Jobs'') IS NULL SELECT Job_ID
FROM ?.dbo.Jobs
WHERE PostDate >= ''6/8/2006'' else print ''?'''
(not tested)


Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
 
Oops, yes of course. Thanks.

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
 
I didn't add it first, but it bombs when i tried this query on my Sql Serever :)

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top