读书人

求一条简单SQL语句,该如何解决

发布时间: 2012-02-01 16:58:19 作者: rapoo

求一条简单SQL语句
判断删除物料编号时判断 当该物料的库存大于0或者有出入库记录时,不能删除。怎么判断?

if not exists(select a.Num from Store a
left join InStockSub b on b.Num=a.Num
left join OutStockSub c on c.Num=a.Num
where a.Num=@Num and (a.Qty> 0 or b.Num is not null or c.Num is not null))

这句话怎么改才合适?

[解决办法]
if exists(select 1 from Store where num=@num and qty> 0)
or exists(select 1 from InStockSub where num=@num)
or exists(select 1 from OutStockSub where num=@num)
begin
raiserror( '该物料有库存或有出入库记录,不能删除! ',16,1)
rollback tran
return
end

读书人网 >SQL Server

热点推荐