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

Multiple Database Query

Status
Not open for further replies.
Jun 16, 2000
199
US
Hi -

I have a table across 150 databases and I want to do a simple select statement and get the results from all companies in one view. I tried a cursor but it doesnt work in the FROM statement. Any ideas on how to be able to select FROM many different databases instead of doing something like this...

select field from Dbase1.dbo.table union all
select field from Dbase2.dbo.table union all
select field from Dbase3.dbo.table

etc. I dont want to have to hard code this and also there is a table that has all of the database ids in it. Thoughts?
 
Hi,

I would create the query dynamically by using a table variable go over each database id using a while loop and add it to the query. Then execute the query.

Y
 
I wopuld suck it up and hard code unless this is something that will only happen on the back end. Dynamic SQl is a very bad thing to use. It is not secure and would require you to allow user permissions at the table level for everyone of these tables opening your database up to possible sql injection or internal fraud. It is also slower that non-dynamic sql.

Read the following link for an understanding of how very bad dynamic SQl is:

"NOTHING is more important in a database than integrity." ESquared
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top