• delphi 并发取数据库id问题


    这段时间有个项目id频繁出现 id冲突的问题 一真找不到原因 后来想到了个办法 在新建取id时先把到到的id保存起来

    上代码 望大神指点下

    /// <summary>
    /// 到表中的最大id
    /// </summary>
    /// <param name="fld">字段名--必须是int型</param>
    /// <param name="tbl">表名</param>
    /// <param name="qrytmp">临时数据集</param>
    /// <param name="RdType">字段</param>
    /// <returns></returns>
    function TMainForm.GetMaxID(fld, tbl: string; qrytmp: tadoQuery; RdType: string): string;
    var
    s, cId, cCondition: string;
    begin
    cCondition := ' where cRdType = ' + quotedstr(RdType);
    
    s := 'select isnull(max(' + fld + '),0) +1 from ' + tbl;
    
    if RdType <> '' then
    s := s + cCondition;
    
    DoQuery(qrytmp, s, true);
    
    cid := qrytmp.Fields[0].AsString;
    
    s := 'select count(1) from ' + tbl;
    if RdType <> '' then
    s := s + cCondition;
    
    DoQuery(qrytmp, s);
    
    if RdType <> '' then
    begin
    if qrytmp.Fields[0].AsInteger <= 0 then
    begin
    s := 'insert yq_GetMaxID (cRdType,cMaxid) values ('
    + quotedstr(RdType) + ',' + cid + ')';
    DoQuery(qrytmp, s, false);
    end else
    begin
    s := 'update yq_GetMaxID set cMaxID = ' + cid + cCondition;
    
    DoQuery(qrytmp, s, false);
    end;
    end;
    
    result := cid;
    end;
    
     
    

      

    yq_GetMaxID是建的一个临时表

    如下:

    create table yq_GetMaxID
    (
    id int identity primary key,
    cRdType nvarchar(32) default '',--出入库类型
    cMaxId int default 0 --这个地方应是i开头
    )
    

      

    执行sql语句如下:

    /// <summary>
    /// 执行sql语句
    /// </summary>
    /// <param name="adoquery">数据集</param>
    /// <param name="strSQL">sql语句</param>
    /// <param name="bOpen">是否执行open</param>
    procedure TMainForm.DoQuery(var adoquery: TADOQuery; strSQL: string; bOpen: boolean);
    begin
    adoquery.close;
    adoquery.sql.clear;
    adoquery.sql.add(strSQL);
    if bopen then
    begin
    adoquery.Open;
    end else
    adoquery.ExecSQL;
    
    end;
    

      

  • 相关阅读:
    Java实现 LeetCode 284 顶端迭代器
    Java实现 LeetCode 284 顶端迭代器
    Java实现 LeetCode 283 移动零
    Java实现 LeetCode 283 移动零
    Java实现 LeetCode 283 移动零
    Java实现蓝桥杯VIP 算法训练 阶乘末尾
    nginx自定义模块编写-根据post参数路由到不同服务器
    nginx location的管理以及查找
    nginx的请求接收流程(二)
    nginx的请求接收流程(一)
  • 原文地址:https://www.cnblogs.com/SoftWareIe/p/4437003.html
Copyright © 2020-2023  润新知