日期:2014-05-19  浏览次数:20454 次

求两句SQL
入库 
ID,PiD,Num,SysDate
1,   1,   50,   2007-4-9
2,   1,   50,2007-4-10
     
现在出库Pid   =   1,   Num   =   70;
出库按时间先后出库.

出库 
ID,Pid,Num,SysDate
1,   1,   100,2007-4-10


出库后   入库表 结果:
ID,PiD,Num,SysDate
1,   1,   0,   2007-4-10
2,   1,   30,2007-4-11

有一个出入库对应表
OutID,InID,   Num
1,   1,50
1,   2,20


出库表修改 数量时,如何对入库表进行更新?
我的办法是:先删除再插入,不知道有没有高招?
Sql2005   数据库

删除一个出库记录时(要更新入库表),麻烦高手写一个效率比较高的SQL语句。

不知道说明白了没有?

------解决方案--------------------
考虑到多条出库记录的情况,估计用游标处理起来会比较容易。
------解决方案--------------------
删除的同时做更新,建议用存储过程实现,使用事务保证数据处理的完整性。

用游标。

------解决方案--------------------
1.先删除再插入效率比较低。
lz可以考虑用更新触发器,在入库表更新时,根据条件根据出库表。
2.在出库表建删除触发器,删除出库表时,根据条件更新入库表
------解决方案--------------------
TO: CathySun118(斯年) ( ) 信誉:100
1.先删除再插入效率比较低。
lz可以考虑用更新触发器,在入库表更新时,根据条件根据出库表。
2.在出库表建删除触发器,删除出库表时,根据条件更新入库表
-----------------------
支持用触发器实现数据的同步
------解决方案--------------------
都已经写存储过程了,还用触发器?
事务,存储过程,我处理此类问题的方法

------解决方案--------------------
while @OutNum> 0
begin
update [入库表] set case when @OutNum> Num then @outnum=@outnum-num,num=0 else num=num-@OutNum end
where num> 0 and SysDate=(select Min(SysDate) from [入库表] where num> 0 and pid=1 )
end

try try 我现在没SQL试
------解决方案--------------------
CREATE TABLE intb
(
ID int,
PID int,
Num int,
SysDate datetime
)
CREATE TABLE outtb
(
ID int,
PID int,
Num int,
SysDate datetime
)
CREATE TABLE inouttb
(
OutID int,
InID int,
Num int
)
insert into intb
select 1, 1, 50, '2007-4-9 ' union all
select 2, 1, 50, '2007-4-10 '
insert into outtb
select 1, 1, 50, '2007-4-9 ' union all
select 2, 1, 50, '2007-4-10 '
insert into inouttb
select 1, 1,50 union all
select 1, 2,20
go
create trigger tritest
on outtb
for update
as
declare @num int --改动的数量
declare @id int --入库id
declare @leave int --剩余数量
--选择更新前与更新后数量的差到@num中
select @num=sum(c.num)-inserted.Num from outtb a inner join inouttb b on a.id=b.OutID
inner join intb c on b.InID=c.id where a.id=inserted.id order by c.SysDate desc
if @num> 0 --减小出库数量时
begin
while @Num> 0
begin
select top 1 @id=c.id,@leave=b.Num-@Num from outtb a inner join inouttb b on a.id=b.OutID
inner join intb c on b.InID=c.id where a.id=inserted.id and c.num=0 order by c.SysDate desc
if @leave> 0
begin
update intb set Num=@leave where id=@id
set @Num=0
end
else
begin
update intb set Num=b.Num,@Num=@Num+@leave
from intb a inner join inouttb b on a.id=b.inid and a.id=@id
end
end
end
else --增大出库数量时
begin
--与上个是相反的过程
end
------解决方案--------------------
--不知道我理解的对不
create table tbname(ID int,PiD int,Num int,SysDate datetime)