当我执行代码时:
declare @result int
exec recostcos @result
select @result
我得到消息208,级别16,状态0,程序RecostCOS,第138行 无效的对象名称'#rLB'。'
奇怪的是第一次引用#rLB没有产生任何错误,但第138行的第二个引用产生了上面的错误。 请在下面找到recostcos的代码:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[RecostCOS](@result int output)
as
BEGIN TRY
BEGIN TRANSACTION
SET NOCOUNT ON
--variables
declare @itemName varchar(50)
declare @invoiceNo varchar(50)
declare @customerName varchar(100)
declare @recordedBy varchar(100)
declare @transDate datetime
declare @supCode varchar(50)
declare @decemQty1 decimal(18,6)
declare @decemQty2 decimal(18,6)
declare @decemQty3 decimal(18,6)
declare @recostQty decimal(18,6)
declare @count int
declare @innerCount int
declare @outputCode int
declare @outputCounter int
declare @recId int
declare @innerId int
create table #supcodes(supcode varchar(50),item_name varchar(50),
qty decimal(18,6),id int identity(1,1))
create table #recostList(invoice_no varchar(50),qty decimal(18,6), id int identity(1,1))
create table #rLB(invc_no varchar(50),qty decimal(18,6), id int identity(1,1))
--check for supplyCode needing reprocessing
select @count=count(*) from someTable where recosted=0;
if(@count<=0)
begin
commit
set @result=0
return @result
end
--
insert into #supcodes(supcode,item_name,qty)
select supply_code,item_name,quantity from someTable
where recosted=0
--recost each supply code needing recosting
while(@count>0)
begin
select top 1 @supCode=supcode,@itemName=item_name,@decemQty1=qty,@recId=id
from #supcodes
select @decemQty2=sum(current_qty) from someTable2 where item_name=@itemName
select @decemQty3=quantity from someTable3 where item_name=@itemName
if(@decemQty2 is null)
set @decemQty2=0
if(@decemQty3 is null)
set @decemQty3=0
--debug
if(@decemQty2<>@decemQty3)
begin
--check if there is a log of this error
select @count=count(*) from someTable4 where error_code=1 and
item_name=@itemName and supply_code=@supCode and resolved=0
if(@count<=0)
--error in stock quantities (this must be resolved b4 any recosting)
insert into someTable4(supply_code,item_name,error_code,error_source,error_detail)
values(@supCode,@itemName,1,'re-costing','Mismatch in stock quantities in stock state and supply codes tables')
end
else if(@decemQty1>@decemQty2)
begin
--check if there is a log of this error
select @count=count(*) from someTable4 where error_code=2 and
item_name=@itemName and supply_code=@supCode and resolved=0
--insufficient stock for recosting
if(@count<=0)
insert into someTable4(supply_code,item_name,error_code,error_source,error_detail)
values(@supCode,@itemName,2,'re-costing','insufficient stock for recosting')
end
else
begin
--recost cost of sales of item involved
--get list of invoices of item to be recosted
set @recostQty=@decemQty1
insert into #recostList(invoice_no,qty)
select invoice_no,quantity from someTable5
where supply_code=@supCode and item_name=@itemName
insert into #rLB(invc_no,qty) select invoice_no,qty from #recostList
--delete cost of sales relating to supcod and item in stock account
select @innerCount=count(*) from #recostList
while(@innerCount>0)
begin
select top 1 @invoiceNo=invoice_no,@decemQty2=qty,@innerId=id
from #recostList
delete someTable6 where description
like 'sales of '+ltrim(rtrim(convert(varchar(20),@decemQty2)))+'%'+
@itemName+'%'+@invoiceNo
delete someTable5 where supply_code=@supCode and item_name=@itemName
and quantity=@decemQty2
delete #recostList where id=@innerId
set @innerCount=@innerCount-1
end
--call costByFIFO to recost item
select @innerCount=count(*) from #rLB
set @outputCounter=@innerCount
while(@innerCount>0)
begin
select top 1 @invoiceNo=invc_no,@decemQty2=qty,@innerId=id
from #rLB
select @customerName=customer_name, @transDate=trans_date,
@recordedBy=recorded_by from someTable7 where invoice_no=@invoiceNo
exec @outputCode=costByFIFO @itemName,@invoiceNo,@customerName,
@decemQty2,@transDate,@recordedBy
--ensure each invoice is costed or reverse entire process
if(@outputCounter=0)
begin
set @outputCounter=@outputCounter-1
end
else
begin
set @result=3 --failed to cost all invoices involved
rollback
end
delete #rLB where id=@innerId
set @innerCount=@innerCount-1
end
--outputCounter must be 0 to indicate all invoices where costed
if(@outputCounter<>0)
begin
set @result=3 --failed to cost all invoices involved
rollback
end
else
begin
update someTable set recosted=1 where supply_code=@supCode
and item_name=@itemName
end
end
delete #supcodes where id=@recId
set @count=@count-1
end
SET NOCOUNT OFF
drop table #supcodes
drop table #recostList
drop table #rLB
set @result=0
COMMIT
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() ErrorNBR, ERROR_SEVERITY() Severity,
ERROR_LINE () ErrorLine, ERROR_MESSAGE() Msg
set @result=2 --unexpected error occured
ROLLBACK
END CATCH