如何在此代码中插入另一个UPDATE SQL命令,该代码使用另一个SQL命令删除一个或多个记录

时间:2012-10-02 12:57:51

标签: sql asp-classic vbscript

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

实际上,如果代码是为了一次删除一条记录而实现的话,这应该不是问题。但是,这个删除代码用于删除多个记录,根据我的技巧,我无法弄清楚如何添加另一个更新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 & "'"  )   



(工作代码)
使用@John提供的解决方案,它可以将qty更新回数据库,以便选择一个/多个记录。

以下是已删除添加')'

的工作代码
<%
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 个答案:

答案 0 :(得分: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
%>