Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2010-11-23 : 12:50:55
|
Hello guys,I have multiple databases with same table structures,I need an output with database name, sum of NumTran between given two date range.How do we get this kind of output? declare @tbl1 table (TranDate date,NumTran int)INSERT INTO @tbl1SELECT '01/10/2009', 10INSERT INTO @tbl1SELECT '02/11/2009', 200INSERT INTO @tbl1SELECT '03/12/2009', 450select SUM(NumTran) as SumTran from @tbl1 where TranDate between '01/01/2009' and '03/30/2009'DatabaseName,NumTranCountAbc001 660Abc002Abc003Abc004 |
|
X002548
Not Just a Number
15586 Posts |
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2010-11-23 : 15:42:56
|
Yes i know all the databases name are stored on sys.databases, the main problem is i used the culsor i have more than thusands dbs so it is taking long time (each db has also millions records)anybody has good solutions pls. |
 |
|
TimSman
Posting Yak Master
127 Posts |
Posted - 2010-11-23 : 15:52:25
|
You want to search through a thousand of databases, each with the same design, and query the same tables in each of those dbs, where each table has millions of records?Why do you have a thousand of databases that are the same? |
 |
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2010-11-23 : 15:57:05
|
yes, because it is using one application for all customers, each customer has own db, but all are similar structure. |
 |
|
TimSman
Posting Yak Master
127 Posts |
Posted - 2010-11-23 : 16:02:26
|
I don't know about performance, but I guess you could use a WHILE loop, build a dynamic query with the db name, and dump the results into a table, then after it's all said and done, query the results.But I'm thinking that if you want to query every single database like that, you'd want to run it as a scheduled job when traffic is light. |
 |
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2010-11-23 : 16:07:44
|
i tried to uxd z shild loop and dump the result, the performance is really bad can't do this....we can not query every single db coz the new db is creating by application any time user is entring new job. |
 |
|
|
|
|
|
|