Delphi Concurrent Fetch Database ID problem

Source: Internet
Author: User

This time, there's a project ID that has frequent ID collisions. I couldn't find a reason for that. Save the ID to the new ID

On the code look at the big God pointing under


<summary>
To the maximum ID in the table
</summary>
<param name= "fld" > Field name--Must be int type </param>
<param name= "tbl" > table name </param>
<param name= "qrytmp" > Temporary data set </param>
<param name= "Rdtype" > Fields </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 is a temporary table built.

As follows:

CREATE TABLE Yq_getmaxid
(
ID int identity PRIMARY key,
Crdtype nvarchar (+) default ',--out of storage type
CMAXID int Default 0-This place should start with I
)

Execute the SQL statement as follows:

<summary>
Execute SQL statement
</summary>
<param name= "Adoquery" > Datasets </param>
<param name= "strSQL" >sql statement </param>
<param name= "bopen" > whether to execute 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

Delphi Concurrent Fetch Database ID problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.