/*
*通常遇到新旧行的比较.比如做采购变更单
*但是如果是删除呢?如何处理?
*方法:采用标志,首先保存第一行(旧资料),然后设置插入的变量的条件值
*遇到删除标志,只有一行,设置插入标志,插入
*/
CREATE procedure UF_Rpt_PoitemchgSp(
@Startingpo_num PoNumType=NULL, --nvarchar(10)
@Endingpo_num PoNumType=NULL, --nvarchar(10)
@Startingpo_line PoLineType=NULL,--smallint(2)
@Endingpo_line PoLineType=NULL, --smallint(2)
@Startingchg_num ChgNumType=NULL,--int(4)
@Endingchg_num ChgNumType=NULL,--int(4)
@StartingRecordDate datetime =NULL,
@EndingRecordDate datetime=NULL,
@StartingVend_num nvarchar(10)=NULL, --nvarchar(10)
@EndingVend_num nvarchar(10)=NULL --nvarchar(10)
)
AS
declare @ReportSet table(
Vend_num nvarchar(7)
,Po_num nvarchar(10)
,Chg_num int
,Po_line smallint
,Po_release smallint
,Seq smallint
,Olditem nvarchar(30)
,Newitem nvarchar(30)
,Oldstat nchar(1)
,Newstat nchar(1)
,Oldduedate datetime
,Newduedate datetime
,Oldvend_item nvarchar(30)
,Newvend_item nvarchar(30)
,Oldqty_ordered_conv decimal
,Newqty_ordered_conv decimal
,Oldu_m nvarchar(3)
,Newu_m nvarchar(3)
,RecordDate datetime
,UpdatedBy nvarchar(30)
,action_code nvarchar(1)
)
declare
@Vend_num nvarchar(7)
,@Po_num nvarchar(10)
,@Chg_num int
,@Po_line smallint
,@Po_release smallint
,@Seq smallint
,@Olditem nvarchar(30)
,@Newitem nvarchar(30)
,@Oldstat nchar(1)
,@Newstat nchar(1)
,@Oldduedate datetime
,@Newduedate datetime
,@Oldvend_item nvarchar(30)
,@Newvend_item nvarchar(30)
,@Oldqty_ordered_conv decimal
,@Newqty_ordered_conv decimal
,@Oldu_m nvarchar(3)
,@Newu_m nvarchar(3)
,@RecordDate datetime
,@UpdatedBy nvarchar(30)
,@action_code nvarchar(1)
Declare MyCursor1 CURSOR
local static FOR SELECT p.vend_num,pc.po_num,pc.chg_num,pc.po_line,pc.po_release,pc.sequence,pc.item,pc.stat,pc.due_date,pc.vend_item,pc.qty_ordered_conv,pc.u_m,pc.RecordDate,pc.UpdatedBy,pc.action_code
from
poitmchg pc left outer join po p on pc.po_num=p.po_num
where pc.action_code <>'A'
and ltrim(pc.po_num)>=ltrim(ISNULL(@Startingpo_num,pc.po_num)) AND ltrim(pc.po_num)<=ltrim(ISNULL(@Endingpo_num,pc.po_num))--去掉空格
AND rtrim(pc.chg_num)>=rtrim(ISNULL(@Startingchg_num ,pc.chg_num)) AND rtrim(pc.chg_num)<=rtrim(ISNULL(@Endingchg_num ,pc.chg_num))--增加变更单号删选条件
AND pc.po_num<=ISNULL(@Endingpo_num,pc.po_num)
AND pc.RecordDate>=ISNULL(convert(datetime,@StartingRecordDate),pc.RecordDate)
AND pc.RecordDate<=ISNULL(convert(datetime,@EndingRecordDate),pc.RecordDate)
AND pc.po_line>=ISNULL(@Startingpo_line,pc.po_line)
AND pc.po_line<=ISNULL(@Endingpo_line,pc.po_line)
AND p.vend_num>=ISNULL(@StartingVend_num,p.vend_num) AND p.vend_num<=ISNULL(@EndingVend_num,p.vend_num);
Open MyCursor1
declare @RowCount integer,@i integer;
set @i=1
set @RowCount=@@CURSOR_ROWS
while 1=1
begin
fetch MyCursor1 into
@Vend_num
,@Po_num
,@Chg_num
,@Po_line
,@Po_release
,@Seq
,@Newitem
,@Newstat
,@Newduedate
,@Newvend_item
,@Newqty_ordered_conv
,@Newu_m
,@RecordDate
,@UpdatedBy
,@action_code
if @@fetch_status<>0
break
if @action_code<>'D'--考虑更新状况
begin
if @i=1
begin
set @Olditem=@Newitem
set @Oldstat=@Newstat
set @Oldduedate=@Newduedate
set @Oldvend_item=@Newvend_item
set @Oldqty_ordered_conv=@Newqty_ordered_conv
set @Oldu_m=@Newu_m
set @i=@i+1
end
else
begin
set @i=1
insert into @ReportSet(Vend_num,Po_num,Chg_num,Po_line,Po_release,Seq,Olditem,Newitem,Oldstat,Newstat,Oldduedate,Newduedate,Oldvend_item,Newvend_item,Oldqty_ordered_conv,Newqty_ordered_conv,Oldu_m,Newu_m,RecordDate,UpdatedBy,action_code)
values(@Vend_num,@Po_num,@Chg_num,@Po_line,@Po_release,@Seq,@Olditem,@Newitem,@Oldstat,@Newstat,@Oldduedate,@Newduedate,@Oldvend_item,@Newvend_item,@Oldqty_ordered_conv,@Newqty_ordered_conv,@Oldu_m,@Newu_m,@RecordDate,@UpdatedBy,@action_code)
end
end
else --考虑删除的情况
begin
set @i=1
set @Olditem=@Newitem
set @Oldstat=@Newstat
set @Oldduedate=@Newduedate
set @Oldvend_item=@Newvend_item
set @Oldqty_ordered_conv=@Newqty_ordered_conv
set @Oldu_m=@Newu_m
set
insert into @ReportSet(Vend_num,Po_num,Chg_num,Po_line,Po_release,Seq,Olditem,Newitem,Oldstat,Newstat,Oldduedate,Newduedate,Oldvend_item,Newvend_item,Oldqty_ordered_conv,Newqty_ordered_conv,Oldu_m,Newu_m,RecordDate,UpdatedBy,action_code)
values(@Vend_num,@Po_num,@Chg_num,@Po_line,@Po_release,@Seq,@Olditem,@Newitem,@Oldstat,@Newstat,@Oldduedate,@Newduedate,@Oldvend_item,@Newvend_item,@Oldqty_ordered_conv,@Newqty_ordered_conv,@Oldu_m,@Newu_m,@RecordDate,@UpdatedBy,@action_code)
end
end
close MyCursor1
deallocate MyCursor1
select *
from @ReportSet
where Olditem<>Newitem OR Oldduedate<>Newduedate OR Oldqty_ordered_conv<>Newqty_ordered_conv OR action_code='D'
GO