Solved

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

Posted on 2011-03-01
6
517 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

813 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

9 Experts available now in Live!

Get 1:1 Help Now