各位大虾:
请问如何用sql语句做到查找最大值并且这个值+1
如何用sql语句做到查找最大值并且这个值+1
SELECT MAX(Version) AS version
FROM EDGE_T_Opportunity
WHERE (OpportunityNo = 336)
这个是查找到 我是想在这个基础上把这个version+1写入

解决方案 »

  1.   

    SELECT MAX(Version)+1 AS version FROM EDGE_T_Opportunity WHERE OpportunityNo=336
      

  2.   

    insert into EDGE_T_Opportunity(Version)
    SELECT MAX(Version)+1 AS version FROM EDGE_T_Opportunity WHERE OpportunityNo=336
      

  3.   

    这样?
    SELECT MAX(Version) + 1 AS version
    FROM EDGE_T_Opportunity
    WHERE (OpportunityNo = 336)
      

  4.   

    create table #t (id int)select isnull(max(id),0)+1 as id
    from #t
    where 1=2
    drop table #t
      

  5.   

    --可能表中无数据,需要 isnull()create table #t (id int)select isnull(max(id),0)+1 as id
    from #tdrop table #t
      

  6.   

    SELECT isnull(MAX(Version),0)+1 AS version
    FROM EDGE_T_Opportunity
    WHERE (OpportunityNo = 336)
      

  7.   

    哈哈update a
    set Version=Version+1
    from EDGE_T_Opportunity a 
    WHERE OpportunityNo=336
    and Version=(select max(Version) from EDGE_T_Opportunity where OpportunityNo=336)
      

  8.   

    更可能insert  EDGE_T_Opportunity (OpportunityNo,Version)
    SELECT OpportunityNo,MAX(Version)+1 AS version FROM EDGE_T_Opportunity WHERE OpportunityNo=336