473,394 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Suggestions?: Querying tables from multiple databases

Hi;

I am looking for suggestions about how to solve a problem using tsql.

I have been asked to create a report concerning 4 tables.

Each of the 4 tables is in its own database.

The 4 tables are identical in name and structure.

I would like to query all 4 of these tables and get the results as if
they were one table.

All suggestions welcome, thanks.

Steve

Jul 23 '05 #1
2 8953
Steve wrote:
Hi;

I am looking for suggestions about how to solve a problem using tsql.

I have been asked to create a report concerning 4 tables.

Each of the 4 tables is in its own database.

The 4 tables are identical in name and structure.

I would like to query all 4 of these tables and get the results as if
they were one table.

All suggestions welcome, thanks.


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Are all the databases on the same SQL Server? If so you can refer to
each table using this format:

database_name.owner.table_name

E.g.:

Accounts.dbo.TableOfAccounts

If the other databases are remote you can use the linked server feature
of SQL'r and refer to the other DBs using the linked server alias name.
See the BOL article "Adding a Linked Server" and related articles for
more info.
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQpN05YechKqOuFEgEQKl3gCgip9nRMQ3FWWGFaKQXynjLI iH+owAn11s
KtkkGhGJHroLQJsfHhQL7gaO
=nk3A
-----END PGP SIGNATURE-----
Jul 23 '05 #2
Steve (st**********@yahoo.com) writes:
I am looking for suggestions about how to solve a problem using tsql.

I have been asked to create a report concerning 4 tables.

Each of the 4 tables is in its own database.

The 4 tables are identical in name and structure.

I would like to query all 4 of these tables and get the results as if
they were one table.


In additions to MCFoster's posting, you can combine these four
tables with a UNION query:

SELECT ....
FROM db1.dbo.tb11
UNION ALL
SELECT ....
FROM db2.dbo.tb11
UNION ALL
...

You can also create a view that comprises this SELECT statement, and refer
to that in the query.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Henri | last post by:
Hello, I am quite new to ms-sql and I have a problem : I want to create an SQL request which would copy serveral records from a table in a given database to another table (with exactly the same...
1
by: E Arredondo | last post by:
Hi, I am about migrate from an old program database to MySql (Running under RH LINUX)and I'm wondering which is the best option to do : I currently have one file for each of my modules, (I'm...
6
by: Greg | last post by:
I am working on a project that will have about 500,000 records in an XML document. This document will need to be queried with XPath, and records will need to be updated. I was thinking about...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
5
by: Shane | last post by:
I wonder if someone has any ideas about the following. I am currently producing some reports for a manufacturing company who work with metal. A finished part can contain multiple sub-parts to...
4
by: Dirk Olbertz | last post by:
Hi there, I'm currently about to redesign a database which you could compare with a database for managing a library. Now this solution will not only manage one library, but 100 to 500 of them....
5
by: Brian P. Hammer | last post by:
I have data from multiple SQL tables, some of the tables will only have one row, while others multiple rows. I load a bunch of data from the various tables and add them to a third party grid. With...
5
by: sql_er | last post by:
Guys, I have an XML file which is 233MB in size. It was created by loading 6 tables from an sql server database into a dataset object and then writing out the contents from this dataset into an...
2
by: runway27 | last post by:
i am building a registration page where a user register for a username. i am able to insert this into mysql. the situation is every time data is inserted into example table1 in mysql in database1 for...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.