2012-10-02 37 views
0

我有此代码可用于在用户从复选框选择顺序时从订单表中删除一个或多个记录。现在我有库存模块,一旦订单被取消,我希望我的库存记录更新数量(将产品数量添加回当前库存)。如何在此代码中插入另一个UPDATE SQL命令,该命令使用另一个SQL命令删除一个或多个记录

实际上,如果代码实现一次删除一条记录,这应该不成问题。但是,这个删除代码是用于删除多个记录,在我的技能中,我无法弄清楚如何添加另一个更新的Sql命令。所以你可以帮我。非常感谢你。

下面是我现有的代码..

<% 
    call navigation 
    url = main_area & "?" & "page=" & page_current 
    return_page = "../backend/" & url 
    req_status = request("type") 
    if req_status = "restore" then 
     req_status = False 
    else 
     req_status = True 
    end if 

    record = request("bill_id") 
    timestamp = now() 
    record = trim(record) 
    if len(record) > 3 then ' multiple records was selected 
     arrVals = split(record,",")    


     strSql = "" 
     strSql = strSql & "DELETE * FROM tbl_bill_total WHERE "  
     for i = 0 to ubound(arrVals) 
      if i = 0 then   
       strSql = strSql & "bill_id IN ("& trim(arrVals(i)) & " " 
      else 
       strSql = strSql & ","& trim(arrVals(i)) & "" 
      end if 
     next 
     strSql = strSql & ") " 

     strSql2 = strSql2 & "DELETE * FROM tbl_order WHERE "  
     for t = 0 to ubound(arrVals) 
      if t = 0 then 
       strSql2 = strSql2 & " tbl_order.bill_id IN ("& trim(arrVals(t)) & " " 
      else 
       strSql2 = strSql2 & ","& trim(arrVals(t)) & "" 
      end if 
     next 
     strSql2 = strSql2 & "); "  

    else 

     strSql = "DELETE * FROM tbl_bill_total WHERE bill_id=" & record & " " 
     strSql2 = "DELETE * FROM tbl_order WHERE bill_id =" & record & " " 
    end if 




Call DBConnOpen() 
Set Rs = Server.CreateObject("ADODB.Recordset") 
response.write strSql 

conn.Execute strSql 
conn.Execute strSql2 
Call DBConnClose() 

response.redirect return_page 
%> 

,这是我想要添加到SQL语句。由于它需要执行pd_id,我认为这应该在执行上述SQL语句之前执行。

Set rsOrder = conn.Execute("SELECT * FROM tbl_order WHERE bill_id = " & record & "")   
pd_id = rsOrder.fields.item("pd_id") 
od_qty = rsOrder.fields.item("od_qty") 


Set rsInventory = conn.Execute("UPDATE tbl_inventory SET inv_qty_act = inv_qty_act + " & od_qty & ", inv_date = " & date() & " WHERE pd_id = '" & pd_id & "'" ) 



(工作守则)
随着@约翰提供的解决方案现在可以选定一个/多个记录,更新数量回数据库。

下面是消灭了除“)”

<% 
Call DBConnOpen() 
Set Rs = Server.CreateObject("ADODB.Recordset") 

    call navigation 
    url = main_area & "?" & "page=" & page_current 
    return_page = "../backend/" & url 
    req_status = request("type") 
    if req_status = "restore" then 
     req_status = False 
    else 
     req_status = True 
    end if 

    record = request("bill_id") 
    timestamp = now() 
    record = trim(record) 
    if len(record) > 3 then ' multiple records was selected 
     arrVals = split(record,",")    


     strSql = "" 
     strSql = strSql & "DELETE * FROM tbl_bill_total WHERE "  
     for i = 0 to ubound(arrVals) 
      if i = 0 then   
       strSql = strSql & "bill_id IN ("& trim(arrVals(i)) & " " 
      else 
       strSql = strSql & ","& trim(arrVals(i)) & "" 
      end if 
     next 
     strSql = strSql & ") " 

     strSql2 = strSql2 & "DELETE * FROM tbl_order WHERE "  
     for t = 0 to ubound(arrVals) 

     Set rsOrder = conn.Execute("SELECT * FROM tbl_order WHERE bill_id = " & arrVals(t) & "")  
      pd_id = rsOrder.fields.item("pd_id") 
      od_qty = rsOrder.fields.item("od_qty") 
       od_qty = DzToPcs(od_qty) 
     conn.Execute("UPDATE tbl_inventory SET inv_qty_act = inv_qty_act + " & od_qty & ", inv_date = " & date() & " WHERE pd_id = '" & pd_id & "'" ) 

      if t = 0 then 
       strSql2 = strSql2 & " tbl_order.bill_id IN ("& trim(arrVals(t)) & " " 

      else 
       strSql2 = strSql2 & ","& trim(arrVals(t)) & "" 
      end if 
     next 
     strSql2 = strSql2 & "); "  


' response.Write "strSql3 = " & strSql3 
    else 


     Set rsOrder = conn.Execute("SELECT * FROM tbl_order WHERE bill_id = " & record & " ")    
      pd_id = rsOrder.fields.item("pd_id") 
      od_qty = rsOrder.fields.item("od_qty") 
       od_qty = DzToPcs(od_qty) 
     conn.Execute("UPDATE tbl_inventory SET inv_qty_act = inv_qty_act + " & od_qty & ", inv_date = date() WHERE pd_id = '" & pd_id & "'" ) 

     strSql = "DELETE * FROM tbl_bill_total WHERE bill_id=" & record & " " 
     strSql2 = "DELETE * FROM tbl_order WHERE bill_id =" & record & " " 
    end if 




'Call DBConnOpen() --> move to top line 
'Set Rs = Server.CreateObject("ADODB.Recordset") --> move to top line 
'response.write strSql2 

conn.Execute strSql 
conn.Execute strSql2 
Call DBConnClose() 

response.redirect return_page 
%> 

回答

1

我会建议你做库存逆转的循环,像这样的工作代码:

<% 
    call navigation 
    url = main_area & "?" & "page=" & page_current 
    return_page = "../backend/" & url 
    req_status = request("type") 
    if req_status = "restore" then 
     req_status = False 
    else 
     req_status = True 
    end if 

    record = request("bill_id") 
    timestamp = now() 
    record = trim(record) 
    if len(record) > 3 then ' multiple records was selected 
     arrVals = split(record,",")    


     strSql = "" 
     strSql = strSql & "DELETE * FROM tbl_bill_total WHERE "  
     for i = 0 to ubound(arrVals) 
      if i = 0 then   
       strSql = strSql & "bill_id IN ("& trim(arrVals(i)) & " " 
      else 
       strSql = strSql & ","& trim(arrVals(i)) & "" 
      end if 
     next 
     strSql = strSql & ") " 

     strSql2 = strSql2 & "DELETE * FROM tbl_order WHERE "  
     for t = 0 to ubound(arrVals) 

Set rsOrder = conn.Execute("SELECT * FROM tbl_order WHERE bill_id = " & arrVals(t)) & "")    
pd_id = rsOrder.fields.item("pd_id") 
od_qty = rsOrder.fields.item("od_qty") 
conn.Execute("UPDATE tbl_inventory SET inv_qty_act = inv_qty_act + " & od_qty & ", inv_date = " & date() & " WHERE pd_id = '" & pd_id & "'" ) 

      if t = 0 then 
       strSql2 = strSql2 & " tbl_order.bill_id IN ("& trim(arrVals(t)) & " " 
      else 
       strSql2 = strSql2 & ","& trim(arrVals(t)) & "" 
      end if 
     next 
     strSql2 = strSql2 & "); "  

    else 

Set rsOrder = conn.Execute("SELECT * FROM tbl_order WHERE bill_id = " & record & "")    
pd_id = rsOrder.fields.item("pd_id") 
od_qty = rsOrder.fields.item("od_qty") 
conn.Execute("UPDATE tbl_inventory SET inv_qty_act = inv_qty_act + " & od_qty & ", inv_date = " & date() & " WHERE pd_id = '" & pd_id & "'" ) 

     strSql = "DELETE * FROM tbl_bill_total WHERE bill_id=" & record & " " 
     strSql2 = "DELETE * FROM tbl_order WHERE bill_id =" & record & " " 
    end if 




Call DBConnOpen() 
Set Rs = Server.CreateObject("ADODB.Recordset") 
response.write strSql 

conn.Execute strSql 
conn.Execute strSql2 
Call DBConnClose() 

response.redirect return_page 
%> 
+0

非常感谢你。当将qty更新回数据库时,此代码正常工作。但是,当我尝试删除多个记录时发现错误。那么你能帮我解决这个问题吗?我认为这可能与strSql2有关,它被新的SQL语句拦截。 (PS.I've已编辑的代码发布到我的问题以供您细读) – Alxan

+0

strsql和strsql2不会因更改而更改,所以我不确定问题出在哪里。你能告诉我你得到了什么错误或什么不应该发生? – johna

+0

你能告诉确切的类型不匹配错误信息,并指出它与哪一行有关。如果它涉及到一个SQL查询,你也可以响应。写入查询并显示。这应该有助于我们解决问题。谢谢。 – johna

相关问题