Sean, Have a look at my complete code
USE msdb
declare @dbname varchar(50);
declare @SQL nvarchar(MAX)='';
declare @AsOfDate datetime = convert(datetime, '12/31/2012');
declare @orgid int = 0;
declare @assets money = 0.00;
declare @liabilities money = 0.00;
declare @netassets money = 0.00;
declare @income money = 0.00;
declare @expense money = 0.00;
declare @dedicated money = 0.00;
declare @Off money = 0.00;
declare @OrgName varchar(50);
DECLARE @dbloop CURSOR;
SET @dbloop= CURSOR
FOR
select name from sys.databases where name NOT IN ('master','model','msdb','tempdb')
AND State_desc = 'ONLINE'
DECLARE Parishes_CURSOR CURSOR
FAST_FORWARD
READ_ONLY
FOR
select OrganizationID from Organization where IsHostOrganization <> 1
OPEN @dbloop
fetch next from @dbloop into @dbname
while (@@FETCH_STATUS <>0)
BEGIN
IF OBJECT_ID(@DBName + N'.dbo.Account') IS NOT NULL
BEGIN
OPEN Parishes_CURSOR
FETCH NEXT FROM Parishes_CURSOR INTO @OrgID
WHILE (@@FETCH_STATUS <> -1)
BEGIN
IF (@@FETCH_STATUS <> -2)
BEGIN
IF ( @@FETCH_STATUS <> 0 )
BREAK
SET @OrgName = N'SELECT ORG_ID ' + CONVERT(varchar(10), @orgid) +' '+ '[Description]' + ' ' +'('+ CONVERT(varchar(20),+ 'pol_id)'
+')'+ 'from'+ @dbname + '.dbo.Organization'+ 'where'+ 'OrganizationID' +'='+ orgid;
drop table #AcctBal;
create table #AcctBal(AccountID int, Balance money);
insert #AcctBal(AccountID, Balance)
exec resultset_AccountEndingBalance @orgid,@AsOfDate;
SET @assets= N'select ' + 'SUM'+'('+'isnull'+'('+'Balance'+','+ 0+')'+')'+' from' + '#AcctBal' +'INNER '+ 'JOIN'+ @dbname+'.dbo.Account' +'ON' +'#AcctBal.AccountID '+'='+ 'Account.AccountID'
+' where'+' Account.AccountType'+ '=' +1+ 'and ' +'RecordStatus'+ '='+ '1';
SET @liabilities = N'select ' + 'SUM'+'('+'isnull'+'('+'Balance'+','+ 0+')'+')'+' from' + '#AcctBal' +'INNER '+ 'JOIN'+ @dbname+'.dbo.Account'+ 'ON' +'#AcctBal.AccountID'+' =' +' Account.AccountID'
+' where'+' Account.AccountType'+ '=' +2+ 'and ' +'RecordStatus'+ '='+ '1';
SET @netassets= N'select ' + 'SUM'+'('+'isnull'+'('+'Balance'+','+ 0+')'+')'+' from' + '#AcctBal' +'INNER '+ 'JOIN'+ @dbname+'.dbo.Account' +'ON' +'#AcctBal.AccountID '+'='+ 'Account.AccountID'
+' where'+' Account.AccountType'+ '=' +3+ 'and ' +'RecordStatus'+ '='+ '1';
SET @income= N'select ' + 'SUM'+'('+'isnull'+'('+'Balance'+','+ 0+')'+')'+' from' + '#AcctBal' +'INNER '+ 'JOIN'+ @dbname+'.dbo.Account' +'ON' +'#AcctBal.AccountID '+'='+ 'Account.AccountID'
+' where'+' Account.AccountType'+ '=' +4+ 'and ' +'RecordStatus'+ '='+ '1';
SET @expense= N'select ' + 'SUM'+'('+'isnull'+'('+'Balance'+','+ 0+')'+')'+' from' + '#AcctBal' +'INNER '+ 'JOIN'+ @dbname+'.dbo.Account' +'ON' +'#AcctBal.AccountID '+'='+ 'Account.AccountID'
+' where'+' Account.AccountType'+ '=' +5+ 'and ' +'RecordStatus'+ '='+ '1';
SET @dedicated= N'select ' + 'SUM'+'('+'isnull'+'('+'Balance'+','+ 0+')'+')'+' from' + '#AcctBal' +'INNER '+ 'JOIN'+ @dbname+'.dbo.Account' +'ON' +'#AcctBal.AccountID '+'='+ 'Account.AccountID'
+' where'+' Account.AccountType'+ '=' +6+ 'and ' +'RecordStatus'+ '='+ '1';
if (@dedicated is null)
set @dedicated = 0.00;
--print 'Assets ' + Convert(varchar(20), @assets);
--print 'Liabilities ' + Convert(varchar(20), @liabilities);
--print 'Net Assets ' + Convert(varchar(20), @netassets);
--print 'Income ' + Convert(varchar(20), @income);
--print 'Expense ' + Convert(varchar(20), @expense);
--print 'Current Period Change ' + Convert(varchar(20), (@income - @expense));
--print 'Dedicated ' + Convert(varchar(20), @dedicated);
set @Off = @assets - (@liabilities + @netassets + @dedicated + (@income - @expense));
if (@Off <> 0.00)
SET @SQL= @SQL + @OrgName + ' is out of balance by ' + convert(varchar(30), @Off) + CHAR(13) + CHAR(10) ;
print @SQL;
END
FETCH NEXT FROM Parishes_CURSOR INTO @OrgID
END
CLOSE Parishes_CURSOR
DEALLOCATE Parishes_CURSOR
End
fetch next from @dbloop into @dbname
END
CLOSE @dbloop
deallocate @dbloop
--Pra:-):-)--------------------------------------------------------------------------------