Solved

How to perform query in same table name but in multiple database

Posted on 2011-03-01
6
492 Views
Last Modified: 2012-05-11
what is the best way for me to query table with same table name but located in multiple db's in the same server ?

Here is my current scripts which only extract only in table and one db.

select ColumnA, count(*) as count from TableTest44 with ( nolock )
where (((sid = 1 and eventid = 1) AND nname <> '')
AND (datetime >= '2010-08-01 00:00:00.000' and datetime <'2010-08-31 23:59:59.999'))
AND (username NOT LIKE 'samkry%'
0
Comment
Question by:motioneye
6 Comments
 
LVL 10

Expert Comment

by:Asim Nazir
ID: 35005530
0
 
LVL 10

Expert Comment

by:Asim Nazir
ID: 35005576
In simple words, you need to use fully qualified names including db name for querying DB. e.g.

select *
from dbo.ThisTable t1
join ThatDatabase.dbo.ThatTable t2
on t1.KeyColumn = t2.KeyColumn

0
 
LVL 8

Accepted Solution

by:
Anurag Agarwal earned 250 total points
ID: 35005986
hello you only need to give full details of table in this case like

Database_Name.Schema_Name.Table_name for refering a table in different database in same server
 for eg if u got TableTest44 in 3 different databse say DB1,DB2,DB3


select ColumnA, count(*) as count from DB1.dbo.TableTest44 with ( nolock )
where (((sid = 1 and eventid = 1) AND nname <> '')
AND (datetime >= '2010-08-01 00:00:00.000' and datetime <'2010-08-31 23:59:59.999'))
AND (username NOT LIKE 'samkry%'

select ColumnA, count(*) as count from DB2.dbo.TableTest44 with ( nolock )
where (((sid = 1 and eventid = 1) AND nname <> '')
AND (datetime >= '2010-08-01 00:00:00.000' and datetime <'2010-08-31 23:59:59.999'))
AND (username NOT LIKE 'samkry%'

select ColumnA, count(*) as count from DB3.dbo.TableTest44 with ( nolock )
where (((sid = 1 and eventid = 1) AND nname <> '')
AND (datetime >= '2010-08-01 00:00:00.000' and datetime <'2010-08-31 23:59:59.999'))
AND (username NOT LIKE 'samkry%'

only change in databse will come, normaly schema name is dbo but if you have change it thn use tht, and then your table name
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 50

Expert Comment

by:Lowfatspread
ID: 35006010
what are you trying to do?
  production , ad-hoc, known databases , or you have to determine database names? .... etc

how often do you intend to do it?
  do the results need to be consistent?
      can we run in parallel
         synchronously/asynchronously

...

some background please...

why is the data split like this?
how much data?

can you set up a cross database View?






0
 
LVL 25

Assisted Solution

by:reb73
reb73 earned 250 total points
ID: 35006242
You could also consider using the undocumented stored procedure 'sp_msforeachdb' to dynamically detect and run the query from multiple databases. This is useful if you have the table in quite a few databases..

Sample code is attached below, you will have to structure the global temp table (##qry1) to match the query results and change/adapt the actual query to get what you need.


set quoted_identifier off
if object_id('tempdb..##qry1','u') is not null
begin
	drop table ##qry1
end

create table ##qry1
(
	 DBName	sysname not null
	,Type	char(2) null
	,Cnt	int null
)

declare @sql varchar(1000)
set @sql = "
use ?
if object_id('sysobjects') is not null
begin
	insert  ##qry1 (DBName, Type, Cnt)
	select '?' as dbname, type, count(*) from sysobjects
	group by type
end 
"
exec sp_msforeachdb @sql

select * from ##qry1

drop table ##qry1
set quoted_identifier on

Open in new window

0
 

Author Closing Comment

by:motioneye
ID: 35146800
Tq, its partially help me to fix my problem.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now