如果不想让错误数据影响当天销售,可以建立作业,将错误数据做好备份,让剩余数据正常拆分,建议一天晚上执行一次
if not exists(select * from sysobjects where name='tb_o_sale_err')
begin
CREATE TABLE [dbo].[tb_o_sale_err](
[c_guid] [varchar](36) NULL,
[c_identity] [int] NOT NULL,
[c_pos_identity] [int] NULL,
[c_id] [int] NOT NULL,
[c_computer_id] [int] NOT NULL,
[c_datetime] [datetime] NOT NULL,
[c_cashier] [varchar](10) NOT NULL,
[c_flag] [char](1) NULL,
[c_cardno] [varchar](20) NULL,
[c_adno] [varchar](10) NULL,
[c_gcode] [varchar](13) NOT NULL,
[c_subcode] [varchar](10) NOT NULL,
[c_pt_cost] [money] NULL,
[c_price] [money] NULL,
[c_price_pro] [money] NULL,
[c_price_disc] [money] NULL,
[c_qtty] [decimal](12, 3) NULL,
[c_amount] [money] NULL,
[c_score] [money] NULL,
[c_gds_type] [varchar](20) NULL,
[c_pro_status] [varchar](20) NULL,
[c_present_name] [varchar](40) NULL,
[c_type] [varchar](20) NULL,
[c_seller] [varchar](10) NULL,
[c_charger] [varchar](10) NULL,
[c_in_code] [varchar](20) NULL,
[c_note] [varchar](100) NULL,
[c_store_id] [varchar](20) NULL
) ON [PRIMARY]
end
if exists (select * from tempdb.dbo.sysobjects where id=object_id('tempdb..#tb_o_sale'))
BEGIN
drop table #tb_o_sale
END
select c_guid,MIN(c_identity) as c_identity into #tb_o_sale
from tb_o_sale
where c_identity>(select isnull(c_id,0)
from tb_trans_status where c_proc='销售分拆')
group by c_guid
having count(*)>1
insert into tb_o_sale_err(
c_guid,c_identity,c_pos_identity,c_id,c_computer_id,c_datetime,c_cashier,c_flag,
c_cardno,c_adno,c_gcode,c_subcode,c_pt_cost,c_price,c_price_pro,c_price_disc,c_qtty,
c_amount,c_score,c_gds_type,c_pro_status,c_present_name,c_type,c_seller,c_charger,
c_in_code,c_note,c_store_id
)
select c_guid,c_identity,c_pos_identity,c_id,c_computer_id,c_datetime,c_cashier,c_flag,
c_cardno,c_adno,c_gcode,c_subcode,c_pt_cost,c_price,c_price_pro,c_price_disc,c_qtty,
c_amount,c_score,c_gds_type,c_pro_status,c_present_name,c_type,c_seller,c_charger,
c_in_code,c_note,c_store_id
from tb_o_sale(nolock)
where c_identity>(select isnull(c_id,0) from tb_trans_status where c_proc='销售分拆')
and (exists (select * from tb_o_sg (nolock) where tb_o_sale.c_guid=tb_o_sg.c_guid)
or exists (select c_guid from #tb_o_sale b where tb_o_sale.c_guid=b.c_guid and tb_o_sale.c_identity<>b.c_identity))
delete from tb_o_sale
where c_identity>(select isnull(c_id,0) from tb_trans_status where c_proc='销售分拆')
and (exists (select * from tb_o_sg (nolock) where tb_o_sale.c_guid=tb_o_sg.c_guid)
or exists (select c_guid from #tb_o_sale b where tb_o_sale.c_guid=b.c_guid and tb_o_sale.c_identity<>b.c_identity))
go