sql语句问题
现有两个表,一个是商品批号库存表(含批号)goods.列为 goodid(商品内码),lotid(批号),qty(库存);另一个表是采购表goodsstock,列为goodid,lotid,stockqty(采购数量).
现我想把goodid(商品内码)和批号(lotid)同时对应的采购数量加到批号库存表里,想要一句sql解决问题.请问该怎么写sql?
[解决办法]
try
update goods aset qty = qty + (select stockqty from goodsstock where goodid=a.goodid and lotid=a.lotid group by goodsid,lotid)
[解决办法]
try
update goods aset qty = qty + (select sum(stockqty) from goodsstock where goodid=a.goodid and lotid=a.lotid group by goodid,lotid)
[解决办法]
建议你用触发器,不要说不想用,在管理库存上触发器可以让你的代码工作量少10倍!
CREATE TRIGGER Trg_Update ON YourSourceTableFOR UPDATEAS--对于INSERTED部分if exists( select 1 from YourTargeTable,Inserted where clause…… )begin update YourTargeTable set ColName = ,…… from Inserted where clause……endelsebegin insert into YourTargeTable(……) select colName…… from Insertedend--对于DELETED部分update YourTargeTableset ColName = ,……from deletedwhere clause……