求助
问题描述:   
            同一个产品在成品时分多次入库。出库时希望能按先进先出原则来实现(自动减分批入库数据)。   
    
  入库表的表结构:   
        
        名称       代号          工号          批次         入库数量      库存数据      入库时间   
        a1         ab             ac             p06           30           30          2006-5-12   
        a1         ab             ac             p06           50           50          2006-5-13   
        a1         abc            ac             p06           230         230          2006-5-13   
    
  目的:   
    在出库时候,出a1、ab、ac   数量40   对应出库单号为 a
  入库结果是:   
        名称       代号          工号           批次          入库数量    库存数据        入库时间   
        a1         ab             ac             p06           30          0              2006-5-12   
        a1         ab             ac             p06           50          40             2006-5-13   
        a1         abc            ac             p06           230         230            2006-5-13     将这个变化记入另外一个表 
        名称           代号          工号           批次          出库单号     数量         入库时间   
        a1             ab             ac             p06           a           30          2006-5-12   
        a1             ab             ac             p06           a           10          2006-5-13   
前面有个高人用存储过程编写了出库的动作,代码如下
2 楼LouisXIV(夜游神)回复于 2006-06-22 18:51:03 得分 50
if   exists(select   1   from   sysobjects   where   id=object_id('input')   and   xtype='U')   drop   table   input   
  go   
  create   table   Input   
  (   
  名称   varchar(2),   
  代号   varchar(3),   
  工号   varchar(2),   
  批次   varchar(3),   
  入库数量   int,   
  库存数据   int,   
  入库时间   varchar(10)   
  )   
  insert   into   input   
  select   'a1','ab','ac','p06',30,30,'2006-05-12'   union   all   
  select   'a1','ab','ac','p06',50,50,'2006-05-13'   union   all   
  select   'a1','ab','ac','p06',50,50,'2006-05-14'   union   all   
  select   'a1','abc','ac','p06',230,230,'2006-05-13'   
    
  select   *   from   input   
    
  declare   @a   varchar(2),@b   varchar(3),@c   varchar(2),@d   int   
  select   @a='a1',@b='ab',@c='ac',@d=100   
    
    
    
  declare   @char1   varchar(10),@char2   int   
  select   @char1=min(入库时间),@char2=min(库存数据)   
  from   
  (   
  select   入库时间,(select   sum(库存数据)   from   input   where   名称=a.名称   and   代号=a.代号   and   代号=a.代号   and   入库时间<=a.入库时间)as   库存数据   
  from   input   a   
  where   名称=@a   and   代号=@b   and   工号=@c   
  )a   
  where   库存数据>@d   
    
    
  update   input   
  set   库存数据=0   
  where   入库时间<@char1   
  and   名称=@a   and   代号=@b   and   工号=@c   
    
  update   input   
  set   库存数据=@char2-@d   
  where     
  入库时间=@char1   and   名称=@a   and   代号=@b   and   工号=@c   
    
  select   *   from   input   
    
    
  drop   table   input   
    
  /*   
  名称     代号    工号      批次  入库数量                库存数据                 入库时间                 
  ----     ----   ----      ----   -----------             -----------             ----------     
  a1       ab       ac       p06     30                     30                     2006-05-12   
  a1       ab       ac       p06     50                     50                     2006-05-13   
  a1       ab       ac       p06     50                     50                     2006-05-14   
  a1       abc      ac       p06     230                    230                   2006-05-13   
    
  名称     代号    工号     批次   入库数量                 库存数据                 入库时间                 
  ----   ----   ----   ----   -----------   -----------   ----------     
  a1       ab       ac       p06     30                     0                       2006-05-12   
  a1       ab       ac       p06     50                     0                       2006-05-13   
  a1       ab       ac       p06     50                     30                      2006-05-14   
  a1       abc      ac       p06     230                   230                      2006-05-13   
我没学过存储过程,一时也不知道怎么改动才能得到我想要的,希望哥哥姐姐们帮帮忙,看看如果用存储过程,怎么把后面的这个记入表的动作加上去,或者是不是不用存储过程也能实现?怎么实现?

解决方案 »

  1.   

    我个人倾向于后一种方法,如果两种方法都教教我,最好了,帖子我又追加了100分。先谢谢ling242a 
      

  2.   

    我用的ORACLE数据库,那个时间操作需要改成你自己的,你也可以出库数据作为一个参数,直接传进去,像这样
    Private Function FIFO(strOutNo As String,strName As String,strCode As String,Dim strEmpId As String,dblQty As Double) As BooleanEnd Function
    Private Function FIFO() As Boolean
        Dim strOutNo As String
        Dim strName As String
        Dim strCode As String
        Dim strEmpId As String
        Dim dblQty As Double
        
        Dim strSql As String
        Dim rsTmp As New ADODB.Recordset
        Dim G_AdoConn As New ADODB.Connection
        
        strOutNo = "a"
        strName = "a1"
        strCode = "ab"
        strEmpId = "ac"
        dblQty = 40
        
        FIFO=False
        On Error GoTo ErrMsg
        strSql = "select * from 入库表 where 名称='" & strName _
            & "' and 代号='" & strCode & "' and 工号='" & strEmpId & "' " _
            & "order by 入库时间"
        rsTmp.Open strSql, G_AdoConn, adOpenKeyset, adLockReadOnly '把G_AdoConn改成你的数据库连接
        G_AdoConn.BeginTrans
        Do Until rsTmp.EOF
            If dblQty = 0 Then Exit Do
            If rsTmp("库存数据") >= dblQty Then
                strSql = "update 入库表 set 库存数据=" & rsTmp("库存数据") - dblQty _
                    & " where 名称='" & strName & "' and 代号='" & strCode & "' and 工号='" & strEmpId _
                    & "' and 入库时间=to_date('" & Format(rsTmp("入库时间"), "yyyy/mm/dd") & "','yyyy/mm/dd')"
                G_AdoConn.Execute strSql
                strSql = "insert into 出库记录 (名称,代号,工号,批次,出库单号,数量,入库时间) values('" _
                    & strName & "','" & strCode & "','" & strEmpId & "','" & rsTmp("批次") & "','" _
                    & strOutNo & "'," & dblQty & ",to_date('" & Format(rsTmp("入库时间"), "yyyy/mm/dd") & "','yyyy/mm/dd'))"
                G_AdoConn.Execute strSql
                dblQty = 0
            Else
               strSql = "update 入库表 set 库存数据=0" _
                    & " where 名称='" & strName & "' and 代号='" & strCode & "' and 工号='" & strEmpId _
                    & "' and 入库时间=to_date('" & Format(rsTmp("入库时间"), "yyyy/mm/dd") & "','yyyy/mm/dd')"
                G_AdoConn.Execute strSql
                strSql = "insert into 出库记录 (名称,代号,工号,批次,出库单号,数量,入库时间) values('" _
                    & strName & "','" & strCode & "','" & strEmpId & "','" & rsTmp("批次") & "','" _
                    & strOutNo & "'," & rsTmp("库存数据") & ",to_date('" & Format(rsTmp("入库时间"), "yyyy/mm/dd") & "','yyyy/mm/dd'))"
                G_AdoConn.Execute strSql
                dblQty = dblQty - rsTmp("库存数据")
            End If
            rsTmp.MoveNext
        Loop
        If dblQty > 0 Then
            MsgBox "没有这么多库存!"
            G_AdoConn.RollbackTrans
            Exit Sub
        End If
        G_AdoConn.CommitTrans
        FIFO=True
        rsTmp.Close
        Set rsTmp = Nothing
        Exit Sub
    ErrMsg:
        G_AdoConn.RollbackTrans
        MsgBox Err.Description
    End Function
      

  3.   

    好了,看懂了,ling242a ,真是谢谢你啊,加分结贴