declare @a varchar(100) select top 1 @a = keyField from yourTable order by keyField desc Select @a
select top 1 .......... order by ...desc你在程序中可以用ado连接,然后ado-》last()
1。最后记录的关键字是最大的: select max(关键字) from table1 2。关键字不是最大的,若有日期时间字段,取日期时间字段最大的关键字: select 关键字 from table1 where 日期时间 = (select max(日期时间) from table1) 3。若不是前两个,就比较麻烦了,要用临时表。 select *, identity(int,1,1) id into #t from table1 select 关键字 from table1 where id = (select max(id) from table1)
select isnull(max(这个字段),convert(varchar(10),getdate(),112) + '-01') from yourtable where case when left(这个字段 ,charindex('-',这个字段 )-1)=convert(varchar(10),getdate(),112)
select convert(char(8),getdate(),112)+right('0'+rtrim(isnull(max(replace(convert(char(8),getdate(),112)+'-',字段,'')),0)+1),2) 结果 from 表 where 字段 like convert(char(8),getdate(),112)+'%'
修改一下 select isnull(max(这个字段),convert(varchar(10),getdate(),112) + '-00') from yourtable where case when left(这个字段 ,charindex('-',这个字段 )-1)=convert(varchar(10),getdate(),112)
我用Delphi是这样实现的,希望能帮到你: 用一个数据集控件如ADOQuery,设置SQL语句为 SELECT TOP 1 字段 FROM table ORDER BY 主键字段然后Open后 去ADOQuery.FieldByName('字段').value就是你要的值
declare @a varchar(50) select top 1 @a = keyField from yourTable order by keyField desc Select @a
select top 1 @a = keyField from yourTable order by keyField desc
Select @a
select max(关键字) from table1
2。关键字不是最大的,若有日期时间字段,取日期时间字段最大的关键字:
select 关键字 from table1 where 日期时间 = (select max(日期时间) from table1)
3。若不是前两个,就比较麻烦了,要用临时表。
select *, identity(int,1,1) id into #t from table1
select 关键字 from table1 where id = (select max(id) from table1)
select isnull(max(这个字段),convert(varchar(10),getdate(),112) + '-00') from yourtable where case when left(这个字段 ,charindex('-',这个字段 )-1)=convert(varchar(10),getdate(),112)
用一个数据集控件如ADOQuery,设置SQL语句为
SELECT TOP 1 字段 FROM table ORDER BY 主键字段然后Open后
去ADOQuery.FieldByName('字段').value就是你要的值
select top 1 @a = keyField from yourTable order by keyField desc
Select @a