我决定改述我的问题以使其更容易一些。 我有一个包含三个字段的表单
<form class="contact_form" action="divProgramProcess.asp" method="post" name="contact_form">
Couch: <input type="text" name="Couch_Current" id="Couch_Current" />
<br/>
Available:
<input type="text" name="Available_Current" id="Available_Current" />
<br/>
Shipment:
<input type="text" name="Shipment_Current" id="Shipment_Current" />
<input type="submit" value="submit" ></form>
表单调用以下代码divProgramProcess.asp(如下所示)
<%
divrec = request.QueryString("div")
Set rstest = Server.CreateObject("ADODB.Recordset")
rstest.locktype = adLockOptimistic sql = "SELECT top 1 * FROM CensusFacility_Records WHERE division_program = 'Division 1' and jmsday ='Sun' order by JMSUpdateDateTime desc "
rstest.Open sql, db
%>
<%
Shipment_Current = request.form("Shipment_Current")
Couch_Current = request.form("Couch_Current")
Available_Current = request.form("Available_Current")
rstest.fields("Shipment") = Shipment_Current
rstest.fields("Couch") = Couch_Current
rstest.fields("Available") = Available_Current
rstest.update
Response.Redirect("chooseScreen.asp")
%>
现在请记住,“JMSUpdateDateTime”列是一个日期字段,division_program列名下有多个“Division 1”记录。 我正在尝试更新SQL表中的最后一行,但每次运行此脚本时,我只更新第一条记录,其中“Division 1”不是最后一条记录。简单地说:有10个记录包含Division 1.所有记录都有日期时间戳列。如何更新最后一条记录?
答案 0 :(得分:2)
不是通过记录集更新,而是运行实际的UPDATE语句。基本上你需要两个步骤:
SELECT TOP 1 ...
查询,但只从表中检索主键/标识字段(不是“*”)并将其保存到某个临时变量中"UPDATE CensusFacility_Records SET Shipment = ... WHERE TABLE_ID = " & yourTempVariable
(最好是创建一个参数化查询,而不是添加参数来查询字符串内联)。这样您就可以保证真正更新特定记录。
答案 1 :(得分:1)
试试这个:
SELECT * FROM CensusFacility_Records WHERE division_program = 'Division 1'
and jmsday ='Sun' and JMSUpdateDateTime = (SELECT MAX(JMSUpdateDateTime)
FROM CensusFacility_Records WHERE division_program = 'Division 1' and jmsday ='Sun')"