如何从ASP中的SQL中获取最新记录

时间:2013-08-23 17:58:14

标签: sql asp-classic

所以这让我发疯了。我正在使用以下查询

SELECT *
FROM
    CensusFacility_Records
WHERE
    Division_Program ='Division 1'
ORDER by JMS_UpdateDateTime DESC

我正在努力获得最新记录。请记住,Division_Program字段中有多个“Division 1”行。我只需要获取今天包含“Division 1”的最新记录。

JMS_UpdateDateTime字段使用月,日,年和时间格式填充时间戳(即8/23/2013 8:00:05 AM)

如何从今天开始获取最新记录?'

我正在更新我的问题。我正在写信写入表中的最新记录。

当我查看表格时,最新记录未更新

<%  
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' ORDER BY JMS_UpdateDateTime DESC"
rstest.Open sql, db
%>

<%
Shipment_Current = request.form("Shipment_Current")
Closed_Bed_Current = request.form("Closed_Bed_Current")
Available_Current = request.form("Available_Current")

rstest.fields("Shipment") = Shipment_Current
rstest.fields("Closed_Bed") = Closed_Bed_Current
rstest.fields("Current") = Available_Current
rstest.update
Response.Redirect("chooseScreen.asp")

%>

3 个答案:

答案 0 :(得分:1)

您可以使用您执行的相同查询,但添加Select Top 1

"SELECT TOP 1 * FROM CensusFacility_Records WHERE Division_Program ='Division 1' ORDER by JMS_UpdateDateTime desc "

答案 1 :(得分:1)

据我所知,TOP关键字正是您所寻找的。您是否介意指定要查询的MSSQL服务器版本?此解决方案仅适用于您的数据以有效的约会格式(如时间戳)存储,如果您的数据以文本格式存储,您只需要在对其进行排序之前进行转换。

SELECT TOP 1 *
FROM
    CensusFacility_Records
WHERE
    Division_Program ='Division 1'
ORDER BY
    JMS_UpdateDateTime DESC

如果您有任何问题,请随时发表评论! : - )


修改

好的,现在这是一个ASP问题!我从来没有做过经典的ASP,但是受到几分钟教程的启发,我建议使用这种方法:

  1. 从SQL select
  2. 中获取对象的主键
  3. 构建以下更新查询
  4. 执行更新查询
  5. 查询

    UPDATE CensusFacility_Records SET
        Shipment = @Shipment_Current,
        Closed_Bed = @Closed_Bed_Current,
        Current = @AvailableCurrent
    WHERE CensusFacility_Records.ID = @ID
    

    使用ASP脚本可能会重新组合:

    sql = "UPDATE CensusFacility_Records SET "
    sql = sql & "Shipment ='" & Request.Form("Shipment_Current") & "'," &
    sql = sql & "Closed_Bed ='" & Request.Form("Closed_Bed_Current") & "'," &
    sql = sql & "Current ='" & Request.Form("AvailableCurrent") &
    sql = sql & "WHERE CensusFacility_Records.ID = " & ID
    conn.Execute sql
    conn.close
    

答案 2 :(得分:0)

您可以使用子查询查找上次日期时间戳,然后加入该子查询。另外,请不要在现实生活中使用SELECT *

SELECT  * 
FROM    CensusFacility_Records CR1
INNER JOIN
        (SELECT CR2.Division_Program
                ,Max(CR2.JMS_UpdateDateTime) as LAST_DATE
        FROM    CensusFacility_Records CR2
        GROUP BY
                CR2.Division_Program
        ) SQ
ON      SQ.Division_Program =  CR1.Division_Program
AND     SQ.LAST_DATE = CR1.JMS_UpdateDateTime
WHERE   CR1.Division_Program ='Division 1'