Hi all,We're running SQL Server 2008 and have run into a bit of a situation. We have 5 databases all with the same tables and we are trying to create a query that will loop through the different databases and output the results per company database. I originally did a cursor, but my boss wants the query to be in a more readable format: His ideal wish would be the query in a stored procedure and the cursor to create the input parameter for the stored procedure for the different databases. Not sure if this is possible? I've tried looking through some forums and googling some possibilities but can't seem to make any sense of them. Any help would be greatly appreciated. Many thanks,Stevan23use masterdeclare @dbname varchar(100),@sql varchar(max)create table #TempDBs (dbname nvarchar(100), Orig_Jnl int, BaseRef int, Posting_Date date, Memo nvarchar(100), Ref1 nvarchar(100), Ref2 nvarchar(100), Jnl_LocTotal float, Jnl_FcTotal float, Jnl_SysTotal float, TransRate float, PostingMonth int, PostingYear int, PostingFinYear int, PostingFinMth int, GFAcctGrp nvarchar(100), GFAcctName nvarchar(100), FthAcctGrp nvarchar(100), FthAcctName nvarchar(100), AcctGrp nvarchar(100), FormatCode nvarchar(100), AcctName nvarchar(100), LineTotal float, FCDebit float, FCCredit float, ContraAct nvarchar(100), LineMemo nvarchar(100), TransType int, ProfitCode nvarchar(10), Line_ID int, BPName nvarchar(100), Dscription nvarchar(100))declare db_cur cursor forSELECT nameFROM master.dbo.sysdatabases where dbid in ('8','18','19','24','25','30')open db_curfetch next from db_cur into @dbnamewhile @@FETCH_STATUS = 0beginset @sql= 'DECLARE @FromDate dateDECLARE @ToDate dateSET @FromDate = ''20150201''SET @ToDate = ''20150228''insert into #TempDbs(dbname,Orig_Jnl,BaseRef,Posting_Date,Memo,Ref1,Ref2,Jnl_LocTotal,Jnl_FcTotal,Jnl_SysTotal,TransRate,PostingMonth,PostingYear,PostingFinYear,PostingFinMth,GFAcctGrp,GFAcctName,FthAcctGrp,FthAcctName,AcctGrp,FormatCode,AcctName,LineTotal,FCDebit,FCCredit,ContraAct,LineMemo,TransType,ProfitCode,Line_ID,BPName,Dscription)select a.DbName, a.Orig_Jnl, a.BaseRef, a.Posting_Date, a.Memo, a.Ref1, a.Ref2, a.Jnl_LocTotal, a.Jnl_FcTotal, a.Jnl_SysTotal, a.TransRate, a.PostingMonth, a.PostingYear, a.PostingFinYear, a.PostingFinMth, a.GFAcctGrp, a.GFAcctName, a.FthAcctGrp, a.FthAcctName, a.AcctGrp, a.FormatCode, a.AcctName, a.LineTotal, a.FCDebit, a.FCCredit, a.ContraAct, a.LineMemo, a.TransType, a.ProfitCode, a.Line_ID, a.BPName, a.DscriptionFROM (SELECT DISTINCT '''+@dbname+''' DbName, t0.TransId, t0.TransType AS Orig_Jnl, t0.BaseRef, t0.RefDate AS Posting_Date, t0.Memo, t0.Ref1, t0.Ref2, t0.LocTotal AS Jnl_LocTotal, t0.FcTotal AS Jnl_FcTotal, t0.SysTotal AS Jnl_SysTotal, t0.TransRate, MONTH(t0.RefDate) AS PostingMonth, YEAR(t0.RefDate) AS PostingYear, (CASE WHEN Month(t0.RefDate) <= 6 THEN Year(t0.RefDate) ELSE Year(t0.RefDate) + 1 END) AS PostingFinYear, (CASE WHEN Month(t0.RefDate) <= 6 THEN Month(t0.RefDate) + 6 ELSE Month(t0.RefDate) - 6 END) AS PostingFinMth, t4.FatherNum As GFAcctGrp, t4.AcctName As GFAcctName, t3.FatherNum As FthAcctGrp, t3.AcctName As FthAcctName, t2.FatherNum As AcctGrp, t2.FormatCode, t2.AcctName, (CASE when t1.TransType = ''13'' then t31.LineTotal when t1.TransType = ''14'' then t32.LineTotal*-1 when t1.TransType = ''18'' then t36.LineTotal when t1.TransType = ''19'' then t37.LineTotal*-1 else t1.Debit - t1.Credit End) As LineTotal, t1.FCDebit, t1.FCCredit, t1.ContraAct, t1.LineMemo, t1.TransType, t1.ProfitCode, t1.Line_ID, isnull(CASE when t1.TransType = ''13'' then t8.CardName when t1.TransType = ''14'' then t9.CardName when t1.TransType = ''18'' then t13.CardName when t1.TransType = ''19'' then t14.CardName End,'''') As BPName, isnull(CASE when t1.TransType = ''13'' then t31.Dscription when t1.TransType = ''14'' then t32.Dscription when t1.TransType = ''18'' then t36.Dscription when t1.TransType = ''19'' then t37.Dscription when t1.TransType = ''30'' then ''Journal Entry'' End,'''') As DscriptionFROM '+@dbname+'.dbo.OJDT t0INNER JOIN '+@dbname+'.dbo.JDT1 t1 ON t0.TransId = t1.TransId LEFT JOIN '+@dbname+'.dbo.OACT t2 ON t1.Account = t2.AcctCodeLEFT JOIN '+@dbname+'.dbo.OACT t3 ON t2.FatherNum = t3.AcctCodeLEFT JOIN '+@dbname+'.dbo.OACT t4 ON t3.FatherNum = t4.AcctCodeLEFT JOIN '+@dbname+'.dbo.OINV t8 ON t1.BaseRef = t8.DocNum and t1.TransType = ''13''LEFT JOIN '+@dbname+'.dbo.INV1 t31 ON t8.DocEntry = t31.DocEntry and t1.Account = t31.AcctCode and t31.TaxOnly = ''N'' and t1.ProfitCode = t31.OcrCodeLEFT JOIN '+@dbname+'.dbo.ORIN t9 ON t1.BaseRef = t9.DocNum and t1.TransType = ''14''LEFT JOIN '+@dbname+'.dbo.RIN1 t32 ON t9.DocEntry = t32.DocEntry and t1.Account = t32.AcctCode and t32.TaxOnly = ''N'' and t1.ProfitCode = t32.OcrCodeLEFT JOIN '+@dbname+'.dbo.OPCH t13 ON t1.BaseRef = t13.DocNum and t1.TransType = ''18''LEFT JOIN '+@dbname+'.dbo.PCH1 t36 ON t13.DocEntry = t36.DocEntry and t1.Account = t36.AcctCode and t36.TaxOnly = ''N'' and t1.ProfitCode = t36.OcrCode and isnull(t36.Project,'''') = isnull(t1.Project,'''')LEFT JOIN '+@dbname+'.dbo.ORPC t14 ON t1.BaseRef = t14.DocNum and t1.TransType = ''19''LEFT JOIN '+@dbname+'.dbo.RPC1 t37 ON t14.DocEntry = t37.DocEntry and t1.Account = t37.AcctCode and t37.TaxOnly = ''N'' and t1.ProfitCode = t37.OcrCodeLEFT JOIN '+@dbname+'.dbo.OVPM t18 ON t1.BaseRef = t18.DocNum and t1.TransType = ''46''WHERE t1.RefDate >= @FromDate and t1.RefDate <= @ToDate and t1.TransType in (''13'',''14'',''18'',''19'',''30'',''46'')) aORDER BY a.AcctName, a.ProfitCode'exec(@sql)fetch next from db_cur into @dbnameendclose db_curdeallocate db_curselect * from #TempDBs order by DbNamedrop table #TempDBs