Hi,
I'm trying to write a store proc like this;
CREATE
procedure [SummaryTest6]/*
BEGIN LOOP
*/
AS
declare
@worksheetname varchar(100)declare
@SQL_Port ASvarchar(1000)declare
@SQL_Loss ASvarchar(1000)declare
@SQL varchar(3000)declare
rREF cursorlocalforselect
p.worksheetname, p.SQL as SQL_Port,l
.SQL as SQL_Lossfrom
adhoc.dbo.tempSQL pjoin
adhoc.dbo.tempSQL lon
p.worksheetname=l.worksheetnamewhere
p.report='Port'and l.report='Loss'order
by p.ordercompletionopen
rREFfetch
nextfrom rREF into @worksheetname, @SQL_Port, @SQL_Losswhile
@@fetch_status=0Begin
SET
@SQL='DECLARE @ReportMonth AS SMALLDATETIME, @FirstOfYear AS SMALLDATETIME
SET @ReportMonth = [ADHOC].[ad-ent\knratna].[DateRoutine](GETDATE(), ''1'' )
SET @FirstofYear = [ADHOC].[ad-ent\knratna].[DateRoutine](GETDATE(), ''5'' )
INSERT INTO [ADHOC].[ad-ent\knratna].[CO_HomeEquity]
select '''
+ @worksheetname +''',p.FileMonth
,sum (AcctCurrentBal)/1000 as Bal
from SRM.dbo.tblPortfolio p
left join SRM.dbo.tblFunded f
on p.CurrentApp = f.AppNumber
where p.FileMonth >= @FirstofYear
and p.FileMonth <= @ReportMonth
'
+ @SQL_Port+'Group by p.FileMonth'
EXEC @SQL
FETCH
NEXTFROM rREF INTO @worksheetname, @SQL_Port, @SQL_LossEND
DEALLOCATE
rREF
when i run this store proc, i get the following error message.
Msg 203, Level 16, State 2, Procedure ks_CO_SummaryTest6, Line 123
The name '
DECLARE @ReportMonth AS SMALLDATETIME, @FirstOfYear AS SMALLDATETIME
SET @ReportMonth = [ADHOC].[ad-ent\knratna].[DateRoutine](GETDATE(), '1' )
SET @FirstofYear = [ADHOC].[ad-ent\knratna].[DateRoutine](GETDATE(), '5' )
INSERT INTO [ADHOC].[ad-ent\knratna].[CO_HomeEquity]
select 'HEQData'
,p.FileMonth
,sum (AcctCurrentBal)/1000 as Bal
,avg(cast(AgeMonths as float)) as Age
'is not a valid identifier.
tempSQL table is as follows;
wokrsheetname SQL
HEQData 2 Port
HEQData 3 Loss
HEQDataX 4 '' and ((p.LineOfBusiness = ''InstLend'' and f.ExceptionFlag = ''Y''' Port
HEQDataX 5 '' and ((au.LineOfBusiness = ''InstLend'' and f.ExceptionFlag = ''Y''' Loss
Can anyone tell me how to fix this error?
Thanks