Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-01
6
Medium Priority
?
566 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 750 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 750 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

886 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