2012-10-04 61 views
0

我有這段代碼用於添加生產數據。由於現在我已經實現了庫存模塊,所以我在這個SO成員的幫助下開始爲每個相關模塊嵌入數量更新函數。現在說到生產模塊,它非常複雜,因爲這個函數必須選擇是使用INSERT還是UPDATE sql來做正確的工作,我試過了,但是沒有成功。所以你可以請我編輯我的代碼。非常感謝你。這段代碼中INSERT或UPDATE記錄的正確SQL語句是什麼

下面是我編輯的代碼失敗:
它可以使用「UPDATE」,但失敗時使用「插入」 SQL

<% 

    pd_id = Request.form("pd_id") 

    pdtn_st_date = Request.form("pdtn_st_date") 
    pdtn_st_note = Request.form("pdtn_st_note") 
    pdtn_st_pdNote = Request.form("pdtn_st_pdNote") 

    qty_fabric_used = Request.form("qty_fabric_used") 
     if qty_fabric_used <> "" then 
      qty_fabric_used = YardToCm(qty_fabric_used) 
     else 
      qty_fabric_used = 0 
     end if 

    unit_per_fb = Request.form("unit_per_fb") 
     if unit_per_fb = "" then 
      unit_per_fb = 0 
     end if 

    is_finished = Request.form("is_finished")  
     if is_finished <> "" then 
      is_finished = True 
     else 
      is_finished = False 
     end if  

    fb_sheet = Request.Form("fb_sheet") 
     if fb_sheet = "" then 
      fb_sheet = 0 
     end if 
    fb_width = Request.Form("fb_width") 
     if fb_width = "" then 
      fb_width = 0 
     end if 

    edit_pdtn_startup = Request.form("edit_pdtn_startup") 

    pdtn_st_qty_act_dz = Request.form("pdtn_st_qty_act") 
     if pdtn_st_qty_act_dz <> "." then 
      pdtn_st_qty_act = DztoPcs(pdtn_st_qty_act_dz) 
     end if  

'<--------------- Update qty to Inventory table here -------> 
Dim con 
Dim rsInventory 

set con=Server.CreateObject("ADODB.Connection") 
con.Provider="Microsoft.Jet.OLEDB.4.0" 
con.Open(Server.Mappath("../database/tkp.mdb")) 

     Set rsInventory = con.Execute("SELECT * FROM tbl_inventory WHERE pd_id = '" & pd_id & "'")    
     if rsInventory.EOF then 
      con.Execute("INSERT INTO tbl_inventory (inv_qty_act, inv_date, pd_id) VALUES (inv_qty_act + " & pdtn_st_qty_act & ", date() ,'" & pd_id & "'" ) 
'   con.Execute("INSERT INTO tbl_inventory (pd_id) VALUES ('" & pd_id & "'") 
     else 

      con.Execute("UPDATE tbl_inventory SET inv_qty_act = inv_qty_act + " & pdtn_st_qty_act & ", inv_date = date() WHERE pd_id = '" & pd_id & "'" ) 
     end if   

'<---------- ----------------------------------------------------------------------------------------> 

    call checkBlank(pd_id) 

    if SomethingError <> "yes" then 

     Call DBConnOpen() 

      Set Rs = Server.CreateObject("ADODB.Recordset") 
      Set Rs.ActiveConnection = Conn 

      strSQL = "SELECT * FROM tbl_pdtn_startup" 
      if edit_pdtn_startup = "Y" then   
       pdtn_st_id = Request.form("pdtn_st_id") 
       strSQL = strSQL & " WHERE pdtn_st_id =" & pdtn_st_id & "" 
       Rs.Open strSQL, Conn, 1, 3  

      pdtn_st_qty_act_dz = Request.form("pdtn_st_qty_act") 
       if pdtn_st_qty_act_dz <> "." then 
        pdtn_st_qty_act = DztoPcs(pdtn_st_qty_act_dz) 
       end if 

      end if 

      if edit_pdtn_startup <> "Y" then  
      Rs.Open strSQL, Conn, 1, 3 
      Rs.AddNew 

      end if 

      Rs.Fields("pdtn_st_date") = pdtn_st_date 
      Rs.Fields("pdtn_st_note") = pdtn_st_note 
      Rs.Fields("pd_id") = pd_id 
      Rs.Fields("pdtn_st_pdNote") = pdtn_st_pdNote 
      Rs.Fields("pdtn_st_qty_act") = pdtn_st_qty_act 
      Rs.Fields("qty_fabric_used") = qty_fabric_used 
      Rs.Fields("unit_per_fb") = unit_per_fb 
      Rs.Fields("is_finished") = is_finished 
      Rs.Fields("fb_width") = fb_width 
      Rs.fields("fb_sheet") = fb_sheet    

      Rs.Update 
      Rs.Close 

     if edit_pdtn_startup = "Y" then 
      response.redirect "production_view.asp?pdtn_st_id=" & pdtn_st_id 
     else 
      strSQL = "SELECT * FROM tbl_pdtn_startup order by pdtn_st_id desc" 
      Rs.Open strSQL, Conn, 1, 3 
      latest_id = Rs("pdtn_st_id") 
      Rs.close 
      response.redirect "production_addSzCl.asp?pdtn_st_id=" & latest_id 
     end if 

     Call DBConnClose() 
    else 
     call writeInputError 
    end if 

session("pdtn_st_id") = pdtn_st_id 
%> 

回答

1

閉架在值列表丟失。這可能是一個問題。 inv_qty_act也不應該在值列表中。

con.Execute("INSERT INTO tbl_inventory (inv_qty_act, inv_date, pd_id) VALUES (inv_qty_act + " & pdtn_st_qty_act & ", date() ,'" & pd_id & "'" ) 

應更正如下

con.Execute("INSERT INTO tbl_inventory (inv_qty_act, inv_date, pd_id) VALUES (" & pdtn_st_qty_act & ", date() ,'" & pd_id & "')") 
+0

是!非常感謝你。它現在工作。 :) – Alxan

相關問題