简体   繁体   中英

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)

<%  
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. 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. To put it simply: There are 10 records that contain Division 1. All of them have date timestamp columns. How can I update the last record?

Instead of updating via recordset, run an actual UPDATE Statement. 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
  2. Run actual 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')"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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