Solved

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

Posted on 2011-03-01
6
530 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL Update query with connected table data 3 41
Upgrading my SSIS package in VS 2012 6 62
Database ERD 4 32
Database Integrity 1 51
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

821 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