如何通过游标获取所有数据库的数量总和
我在 sql 查询中卡在一个地方。我有20个数据库,所有数据库的表名和列名都相同,但数据不同。我正在创建一个 SQL 查询来从所有数据库中获取特定日期的付款金额。
我的付款表中有很多列。我需要有关 paymentTypeID 列的总和(金额),因为我可以根据 paymentTypeID 获取金额。所有 paymentTypeID 在数据库中都不同,但有时是相同的。我为此创建了一个光标。
我的代码是:
create table #tmpDBs(DBName varchar(255), CurrentDayPaymentAmount float)
declare cDBs cursor for
select name from master.dbo.sysdatabases
declare @DB varchar(255)
open cDBs
fetch next from cDBs into @DB
while @@fetch_status = 0
begin
exec('
declare @DateCurrent datetime
set @DateCurrent = '{Current Date}'
insert into #tmpDBs
select DBName = ' + @DB + ',
CurrentDayPaymentAmount = (select sum(p.amount) from ' + @DB + '.dbo.Payment p where p.eDate between @DateCurrent and dateadd(day, 1, @DateCurrent) and (p.paymenttypeid in (14, 15, 16, 17, 21, 22, 24, 35, 37, 38, 50)))
')
fetch next from cDBs into @DB
end
close cDBs
deallocate cDBs
select * from #tmpDBs
drop table #tmpDBs
我使用 In 子句将所有数据库 paymenttypeid 一起传递。 此代码用于从 2 个数据库检索付款金额。 paymenttypeid 14, 15, 16,17, 22, 24, 38 用于第一个数据库,21, 35, 37, 50 用于第二个数据库。我的问题是,当我将这些 paymentTypeID 放在 In 子句中时,它会添加其他数据库的匹配 paymenttypeid 的金额。 例如,如果第一个数据库实际金额为 4589$,但它通过使用第二个数据库 paymenttypeid 添加额外金额(例如 5469$)。如果我只传递一个数据库 paymenttypeid 它会显示正确的金额。
我的查询有什么问题。
I am stucking in one place in sql query. I have 20 databases and all database's table name and columns name are same but data is different. I am creating a sql query to get payment amount of particular date from all databases together.
I have many columns in payment table. I need on sum(amount) regarding there paymentTypeID column because I can get amount on the basis of paymentTypeID. All paymentTypeIDs are different in databases but sometimes it is same. I have created a cursor for this.
My code is:
create table #tmpDBs(DBName varchar(255), CurrentDayPaymentAmount float)
declare cDBs cursor for
select name from master.dbo.sysdatabases
declare @DB varchar(255)
open cDBs
fetch next from cDBs into @DB
while @@fetch_status = 0
begin
exec('
declare @DateCurrent datetime
set @DateCurrent = '{Current Date}'
insert into #tmpDBs
select DBName = ' + @DB + ',
CurrentDayPaymentAmount = (select sum(p.amount) from ' + @DB + '.dbo.Payment p where p.eDate between @DateCurrent and dateadd(day, 1, @DateCurrent) and (p.paymenttypeid in (14, 15, 16, 17, 21, 22, 24, 35, 37, 38, 50)))
')
fetch next from cDBs into @DB
end
close cDBs
deallocate cDBs
select * from #tmpDBs
drop table #tmpDBs
I used In clause to pass all databases paymenttypeid together.
This code is for retrieve the payment amount from 2 databases. paymenttypeid 14, 15, 16,17, 22, 24, 38 is for first database and 21, 35, 37, 50 is for second database. My problem is that when I put these paymentTypeID in In clause it adds the amount of matching paymenttypeid of other database.
For example if first database actual amount is 4589$ but it adds additional amount say 5469$ by using second database paymenttypeid. If I pass only one database paymenttypeid it shows the correct amount.
What is the issue in my query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您自己解释了这个问题:您只需要 DB1 中的类型 1、2 和 3,而 DB2 中只需要类型 4、5 和 6,但是您的查询没有这样做,它要求类型 1、2、3、4 、5和6来自DB1和DB2。
这意味着您需要为每个数据库使用不同的 WHERE 子句,具体取决于您想要的类型。那么,您如何知道每个数据库需要哪些类型以及如何在存储过程中表达该逻辑?
作为完全不同的解决方案,使用正确的 WHERE 子句在每个数据库中创建一个视图,然后只需在该视图中查询报表即可。
I think you explained the issue yourself: you want only types 1, 2 and 3 from DB1 and only types 4, 5 and 6 from DB2, but your query doesn't do that, it asks for types 1, 2, 3, 4, 5 and 6 from DB1 and DB2.
That means you need to use a different WHERE clause for each databases, depending on which types you want. So how do you know which types you want from each database and how can you express that logic in your stored procedure?
As a completely different solution, create a view in each database with the correct WHERE clause and then just query the view for your report.