如何使用sql查询在表中查找最新条目 [英] how to find latest entry in table using sql query

查看:83
本文介绍了如何使用sql查询在表中查找最新条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 cmd.CommandText =   SELECT distinct GLMAST.fglhead,MAINFILE.facno, max(MAINTRAN.fdate)as fdate,max(MAINTRAN.fbalance)as MAINTRAN INNER JOIN MAINFILE MAINFILE.facno MAINTRAN.facno INNER JOIN MASTERID MAINFILE.fmasterno like MASTERID.fmasterid INNER JOIN GLMAST ON GLMAST.fglcode like LEFT (MAINFILE.facno,9)和GLMAST.fglhead ='& ComboBox2。文字&  '和MAINTRAN.fdate< ='&格式(prdate,  MM / dd / yyyy)&  '& _ 
其中MAINTRAN.fbankcode ='& 010& '& _
和MAINTRAN.fdate< ='&格式(prdate, MM / dd / yyyy)& '& _
和MAINTRAN.fbranchcode ='& 01& 'group by MAINFILE.facno,GLMAST.fglhead



 





带cmd

dr = .ExecuteReader

结束



如果dr.HasRows = True那么

Do while dr.Read

presentdaybal = dr!fbalance

循环

dr.Close()

解决方案

< blockquote>插入后直接使用 @@ identity [< a href =http://technet.microsoft.com/en-us/library/ms187342%28v=sql.110%29.aspxtarget =_ blanktitle =New Window> ^ ] property,返回最后插入的标识值。



不要在后面的代码中使用查询。这可能是 SQL注入 [ ^ ]。使用存储过程 [ ^ ]。



Sql Server - 如何在Sql server中编写存储过程 [ ^ ]

演练:仅使用存储过程(Visual Basic) [ ^ ]


如何使用sql查询在表中查找最新条目? okk。



如果你想在你的sql表中选择最新的条目然后使用

  @@ Identity  @ Identity_scope  @@ Identity_current  

(搜索谷歌更多澄清)

如果表有标识栏(AI),它可以工作







没有标识列,但有一个Date列(插入日期)

  top   1  *  > Table_Name 订单  Date_column  desc  





如果您在获取结果时遇到任何其他问题,那么下次发布任何问题时更具体。



你可以更新发布的问题使用改进的问题链接。


cmd.CommandText = "SELECT  distinct GLMAST.fglhead,MAINFILE.facno,max(MAINTRAN.fdate) as fdate ,max(MAINTRAN.fbalance) as fbalance  FROM MAINTRAN INNER JOIN MAINFILE ON  MAINFILE.facno like MAINTRAN.facno INNER JOIN MASTERID ON  MAINFILE.fmasterno like MASTERID.fmasterid  INNER JOIN  GLMAST ON GLMAST.fglcode like  LEFT(MAINFILE.facno,9) and   GLMAST.fglhead = '" & ComboBox2.Text & "' and MAINTRAN.fdate<='" & Format(prdate, "MM/dd/yyyy") & "' " & _
                                          "where MAINTRAN.fbankcode='" & "010" & "'" & _
                                         "and MAINTRAN.fdate<='" & Format(prdate, "MM/dd/yyyy") & "' " & _
                                         "and MAINTRAN.fbranchcode='" & "01" & "' group by MAINFILE.facno,GLMAST.fglhead "




With cmd
dr = .ExecuteReader
End With

If dr.HasRows = True Then
Do While dr.Read
presentdaybal = dr!fbalance
Loop
dr.Close()

解决方案

Direct after insertion, use @@identity[^] property, which returns last inserted identity value.

Do not use queries in code behind. It might be the reason of SQL Injection[^]. Use stored procedure[^] instead.

Sql Server - How to write a Stored procedure in Sql server[^]
Walkthrough: Using Only Stored Procedures (Visual Basic)[^]


how to find latest entry in table using sql query?? okk.

if you want to select the latest entry in your sql table then use

@@Identity,@Identity_scope,@@Identity_current

(search Google for more clarification)
it works if the table has identity column(AI)

or

don't have the identity column but has a Date column(insertion Date)

select top 1 * from Table_Name order by  Date_column desc



if you have any other problem to fetching result then be more specific next time when posting any question.

you can update posted question using improve question link.


这篇关于如何使用sql查询在表中查找最新条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆