简体   繁体   English

如何在ASP中使用SQL更新表的最后一行

[英]How do you update the last row in a table using SQL in ASP

I decided to rephrase my question to make it a bit easier. 我决定改写我的问题,以使其变得容易一些。 I have a form with three fields 我有一个包含三个字段的表格

<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>    

The form calls the following code divProgramProcess.asp (shown below) 该表单将调用以下代码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")
%>

Now keep in mind that the column 'JMSUpdateDateTime" is a date field and there are multiple records of 'Division 1" under the division_program column name. 现在请记住,“ JMSUpdateDateTime”列是一个日期字段,并且在division_program列名称下有多个“ Division 1”记录。 I'm trying to update the last row in my SQL table but everytime I run this script I only update the first record with "Division 1" not the last record. 我正在尝试更新SQL表中的最后一行,但是每次运行此脚本时,我只会使用“分区1”来更新第一条记录,而不是最后一条记录。 To put it simply: There are 10 records that contain Division 1. All of them have date timestamp columns. 简而言之:有10个包含Division 1的记录。所有记录都有date timestamp列。 How can I update the last record? 如何更新最后一条记录?

Instead of updating via recordset, run an actual UPDATE Statement. 而不是通过记录集更新,而是运行实际的UPDATE语句。 Basicaly you will need 2 steps: 基本上,您将需要两个步骤:

  1. Run your SELECT TOP 1 ... query, but retreive just a primary key/identity field from the table (not " * ") and save it into some temp variable 运行SELECT TOP 1 ...查询,但是只检索表中的主键/标识字段(而不是“ *”)并将其保存到某些临时变量中
  2. Run actual SQL "UPDATE CensusFacility_Records SET Shipment = ... WHERE TABLE_ID = " & yourTempVariable 运行实际的SQL "UPDATE CensusFacility_Records SET Shipment = ... WHERE TABLE_ID = " & yourTempVariable

(Best would be to create a parametrized query instead of adding parameters to query string inline). (最好是创建一个参数化查询,而不是向内联查询字符串添加参数)。 This way you're guaranteed to really update specific record. 这样可以保证您确实更新了特定记录。

Try this: 尝试这个:

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')"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM