这用一个ADOQuery即可,头先把你Select 出来的值存入暂存盘中,在依条件比对。你可用以下语法加以修改
CREATE PROCEDURE RP040402 @X varchar(3) AS
create table #temp(area varchar(3),item varchar(9),x3 int,x4 int)
declare @item varchar(9),@item1 varchar,@tran_area varchar(3),@QTY int,@CODE varchar(3),
@x3 int,@x4 int,@area varchar(3)
declare data_cursor cursor for
SELECT PSM10.AREA,PSM10.ITEM,PSWORK2.TRAN_AREA,PSWORK2.CODE,PSWORK2.QTY FROM ITEM,PSM10
LEFT OUTER JOIN PSWORK2 ON PSWORK2.ITEM=PSM10.ITEM AND PSWORK2.AREA=PSM10.AREA
WHERE ITEM.ITEM=PSM10.ITEM
AND (PSM10.I_JOB+PSM10.I_CASTED+PSM10.I_RTN+PSM10.I_CMP+PSM10.I_CUST+PSM10.I_ADJ)+
(PSM10.O_SCR+PSM10.O_CMP+PSM10.O_RTN+PSM10.O_SALES+PSM10.O_EXCHGE+PSM10.O_SAMPLE)+
PSM10.I_ADJ+PSM10.BEGINSTOCK<>0
AND PSM10.AREA=@X
open data_cursor
fetch next from data_cursor into @area,@item,@tran_area,@code,@qty
while @@fetch_status=0
begin
if @tran_area='RTN' and @code='0'
set @x3=@qty
else
set @x3=0
if (@tran_area='RTN' or @tran_area='SCR') and @code='1'
set @x4=@qty
else
set @x4=0
insert #temp(area,item,x3,x4) values (@area,@item,@x3,@x4)
fetch next from data_cursor into @area,@item,@tran_area,@code,@qty
end
select area,item,sum(x3) as sum3,sum(x4) as sum4 into #temp1 from #temp
group by area,item
SELECT PSM20.NAME,PSM30.PRICE,PSM30.ACCCODE,ITEM.WEIGHT,PSM10.ITEM,
PSM10.AREA,PSM10.BeginStock,PSM10.I_JOB,PSM10.I_CASTED,PSM10.I_RTN,PSM10.I_CMP,
PSM10.I_CUST,PSM10.I_ADJ,PSM10.O_SCR,PSM10.O_CMP,PSM10.O_RTN,PSM10.O_SALES,
PSM10.O_EXCHGE,PSM10.O_SAMPLE,PSM10.O_SHIP,PSM10.LASTDATE,PSM10.LASTSHIP,
PSM10.PBEGIN,PSM10.STK,PSM10.I_OTHERS,PSM10.O_OTHERS,PSWORK2.TRAN_AREA,
PSWORK2.VOUCHER,PSWORK2.SEQ,PSWORK2.DATE,PSWORK2.FROM_AREA,PSWORK2.CODE,
PSWORK2.QTY,PSWORK2.ADJCODE,PSWORK2.TO_AREA,PSWORK2.PD_NO,#temp1.sum3,#temp1.sum4
FROM ITEM,PSM10
LEFT OUTER JOIN PSWORK2 ON PSWORK2.ITEM=PSM10.ITEM AND PSWORK2.AREA=PSM10.AREA
LEFT OUTER JOIN PSM20 ON PSM20.AREA=PSM10.AREA
LEFT OUTER JOIN PSM30 ON PSM30.ITEM=PSM10.ITEM AND PSM30.AREA=PSM10.AREA
LEFT OUTER JOIN #TEMP1 ON #TEMP1.ITEM=PSM10.ITEM AND #TEMP1.AREA=PSM10.AREA
WHERE ITEM.ITEM=PSM10.ITEM
AND (PSM10.I_JOB+PSM10.I_CASTED+PSM10.I_RTN+PSM10.I_CMP+PSM10.I_CUST+PSM10.I_ADJ)+
(PSM10.O_SCR+PSM10.O_CMP+PSM10.O_RTN+PSM10.O_SALES+PSM10.O_EXCHGE+PSM10.O_SAMPLE)+
PSM10.I_ADJ+PSM10.BEGINSTOCK<>0
AND PSM10.AREA=@X
ORDER BY PSM10.AREA,PSM10.ITEM
drop table #temp
drop table #temp1
DEALLOCATE DATA_CURSOR