我编写了一个SP,它将在内部调用另一个参数化的SP,并将输出存储到物理表中。当我执行外层SP时,我得到以下错误消息。
消息207,级别16,状态1,过程CBs_LargeExposer,第88行[批处理 起始行12]无效的列名“ SlNo”。
我观察到,如果我在单独的窗口中执行内部SP,并且如果我执行相同的外部SP,则紧挨着它运行良好,如果在一定时间后执行相同的语句(外部SP),则会遇到相同的错误。
ALTER PROCEDURE [dbo].[CBS_GlMapping]
@finYear nvarchar(30)='2019-2020',
@quarter char(5)='Q2',
@Oflag Varchar(6)='O4'
AS
BEGIN
SET NOCOUNT ON;
Declare @QtrStart date,@QtrSEnd date,@FyFrom int,@FyTo int,@BranchId int
select @FyFrom=year(YearBeginDate),@FyTo=Year(YEarEndDate) from BranchTable where BranchCode in(select OrgBankCode from OrgDetails)
IF(@FyFrom < left(@finYear,4) and @FyTo < Right(@finYear,4))
Begin
print 'Sorry, Recods are not available for the financial year ' + @finYear
return
End
If(@quarter='Q1')
Begin
select @QtrStart=YearBeginDate,@QtrSEnd=EOMONTH(DATEADD(MM,2,YearBeginDate)) from BranchTable where BranchCode in(select OrgBankCode from OrgDetails)
End
If(@quarter='Q2')
Begin
select @QtrStart=Dateadd(mm,3,YearBeginDate),@QtrSEnd=EOMONTH(DATEADD(MM,5,YearBeginDate)) from BranchTable where BranchCode in(select OrgBankCode from OrgDetails)
End
If(@quarter='Q3')
Begin
select @QtrStart=Dateadd(mm,6,YearBeginDate),@QtrSEnd=EOMONTH(DATEADD(MM,8,YearBeginDate)) from BranchTable where BranchCode in(select OrgBankCode from OrgDetails)
End
If(@quarter='Q4')
Begin
select @QtrStart=DATEADD(month, DATEDIFF(month, 0,Dateadd(mm,-2,YEarEndDate)), 0),@QtrSEnd=YEarEndDate from BranchTable where BranchCode in(select OrgBankCode from OrgDetails)
End
/* To handel the Financial year */
if(left(@finYear,4)<year(@QtrStart) and @quarter<>'Q4')
begin
set @QtrStart = cast(left(@finYear,4) as varchar(4))+'-'+cast(month(@QtrStart) as Varchar(2))+'-'+Cast(day(@QtrStart) as Varchar(2))
set @QtrSEnd = cast(left(@finYear,4) as varchar(4))+'-'+cast(month(@QtrSEnd) as Varchar(2))+'-'+Cast(day(@QtrSEnd) as Varchar(2))
end
if(right(@finYear,4)<year(@QtrStart) and @quarter='Q4')
begin
set @QtrStart = cast(right(@finYear,4) as varchar(4))+'-'+cast(month(@QtrStart) as Varchar(2))+'-'+Cast(day(@QtrStart) as Varchar(2))
set @QtrSEnd = cast(right(@finYear,4) as varchar(4))+'-'+cast(month(@QtrSEnd) as Varchar(2))+'-'+Cast(day(@QtrSEnd) as Varchar(2))
end
Create table #tempData
(
Bal numeric(15,2)
)
declare @sql varchar(500),@sql2 varchar(500),@day Varchar(10),@Month varchar(2),@Year varchar(4)
select @day= Day(@QtrSEnd)
select @Month= month(@QtrSEnd)
select @Year= Year(@QtrSEnd)
Create table #temp
( slno int,
glcode Varchar(500),
GlLen int,
ColNO varchar(50),
CellNo varchar(50),
Amount Numeric(15,2)
)
declare @tsql varchar(500)
set @tsql=N'
insert into #temp
select ROW_NUMBER() over(order by glcode) slno,glcode,LEN(Glcode) GlLen,ColNo,CellNo,amount
from BsGl'+@Oflag+'
where glcode <>'''' '
exec(@tsql)
declare @LoopStart int,@loopEnd Int,@glcode varchar(500),@amt numeric(15,0)
select @LoopStart=Min(Slno) from #temp
select @loopEnd=MAX(Slno) from #temp
while (@LoopStart <= @loopEnd)
begin
select @glcode= Glcode from #temp where slno = @LoopStart
set @sql='insert into #tempData select Sum(day'+@day+') from DayBal where AcYear='+@Year+' and acmonth='+@Month+'and GlCode in(SELECT glcode FROM dbo.splitstring(convert(varchar(500),'''+@glcode+''')))'
EXEC(@sql)
update #temp set Amount=(select Bal from #tempData) where slno =@LoopStart
set @LoopStart=@LoopStart+1
Truncate table #tempData
end
set @tsql=''
set @tsql='update BsGl'+@Oflag+' set Amount=0.00 where Glcode ='''' '
EXEC(@tsql)
set @tsql=''
set @tsql='update B set B.amount=isnull(round((Case when A.Amount<0 then A.Amount*(-1) else A.Amount end/1000),0),0) from #temp a inner join BsGl'+@Oflag+' B on a.CellNo=B.CellNo and B.ColNo=A.ColNo'
EXEC(@tsql)
IF(@Oflag='O4')
BEGIN
EXEC [DBO].[CBs_LargeExposer] @QtrSEnd
END
Drop table #temp
End
答案 0 :(得分:0)
不确定您是否已经尝试过。尝试在下面的两个SELECT语句中指定与CREATE TABLE语句完全相同的列名“ slno”,而不是“ Slno”。
select @LoopStart=Min(Slno) from #temp
select @loopEnd=MAX(Slno) from #temp
答案 1 :(得分:0)
我找到了错误的解决方案。我在外部sp以及内部sp中使用不同的列创建了相同的#temporary表。基本上,#temporary表的范围仅限于会话,但是当您在外部sp内调用内部Sp时,它将考虑单个会话。
外部SP临时表声明
Create table #tempData
(
Bal numeric(15,2)
)
内部SP临时表声明
CREATE TABLE #tempData(
[ID] int identity(1,1) ,
[Funded] [numeric](15, 2) NOT NULL,
[NonFunded] [numeric](2, 2) NOT NULL,
[Limitsanctioned] [numeric](15, 2) NOT NULL
)
所以我在Sp外部将#tmpData更改为#tempData1,它工作正常。