create proc p_test
as
begin
delete from table where 产品 in(
select 产品 from table group by 产品 having sum(case when 单据='入库' then 数量 when 单据='出库' then -数量 end)=0)
select 产品,单据,sum(case when 单据='入库' then 数量 when 单据='出库' then -数量 end) 数量 from table group by 产品,单据
end