if not exists(select 1 from invstock where batchno=? and wh_code=? and drug_id=? ) begin --如果库存无记录(这里应该怎么处理呢???) insert into invstock(wh_code,mid,totalnum) select wh_code,mid,total from invstock_in_detail where .......... end
错了 drug_id 应该是mid 反正就这个意思
if exists(select 1 from invstock a inner join invstock_in_detail b on a.drug_id=b.drug_id where a.batchno=b.batchno and a.wh_code=@whcode and b.vouchno=@vcode) begin update... end else begin insert... end)
if exists(select 1 from invstock where ....) begin --如果库存有记录 update invstock set totalnum=totalnum+b.total from invstock a inner join invstock_in_detail b on a.drug_id=b.drug_id where a.batchno=b.batchno and a.wh_code=@whcode and b.vouchno=@vcode end else begin --如果库存无记录(这里应该怎么处理呢???) insert into invstock(wh_code,mid,totalnum) select wh_code,mid,total from invstock_in_detail where .......... end
....
esle
...
begin
--如果库存无记录(这里应该怎么处理呢???)
insert into invstock(wh_code,mid,totalnum)
select wh_code,mid,total
from invstock_in_detail
where ..........
end
反正就这个意思
if exists(select 1 from invstock a inner join invstock_in_detail b
on a.drug_id=b.drug_id
where a.batchno=b.batchno and a.wh_code=@whcode and b.vouchno=@vcode)
begin
update...
end
else
begin
insert...
end)
begin
--如果库存有记录
update invstock set totalnum=totalnum+b.total
from invstock a inner join invstock_in_detail b
on a.drug_id=b.drug_id
where a.batchno=b.batchno and a.wh_code=@whcode and b.vouchno=@vcode
end
else
begin --如果库存无记录(这里应该怎么处理呢???)
insert into invstock(wh_code,mid,totalnum)
select wh_code,mid,total
from invstock_in_detail
where ..........
end