Hope someone can clarify something for me, here is my situation:
I am coding in Delphi an application that connects to a SQL Server database.
The application creates boxes on a production floor.
To get the next box number, I need to:
a-Retreive the last box number.
b-Increment this number, if it exceeds 6 digits (1,000,000), set it back to 10,000.
c-Save this new number back to the database.
The issue is that many users could be hitting this setup table and I need to lock the table while I do my incrementation, then release it after I write back to the database.
I am unsure of how to do this, it seems I will need multiple SQL statements.
The code as follows might clear up what I am trying to do, but it of course has no tablelocking / releasing.
strSQL := 'SELECT [Last Entry No] FROM WFSetup';
if odbcStmt.ExecDirect(strSQL) then begin
if odbcStmt.Fetch then begin
dTemp := odbcStmt.GetDataAsDouble('Last Entry No');
Result := dTemp;
dTemp := dTemp + 1;
if dTemp > 1000000 then
dTemp := 100000;
strSQL := 'UPDATE WFSetup' SET [Last Entry No] = ' + FloatToStr(dTemp);
odbcStmt2 := odbcConn.NewStatement;
if not odbcStmt2.ExecDirect(strSQL) then
...
I should point out that in my case SQL Server is being used as a back end for a Navision database, so I cannot use any SQL trigger functionality.
I know that I have to put in the locking somewhere but am unsure on how to do so.
Any help would be appreciated.
Andrew
I am coding in Delphi an application that connects to a SQL Server database.
The application creates boxes on a production floor.
To get the next box number, I need to:
a-Retreive the last box number.
b-Increment this number, if it exceeds 6 digits (1,000,000), set it back to 10,000.
c-Save this new number back to the database.
The issue is that many users could be hitting this setup table and I need to lock the table while I do my incrementation, then release it after I write back to the database.
I am unsure of how to do this, it seems I will need multiple SQL statements.
The code as follows might clear up what I am trying to do, but it of course has no tablelocking / releasing.
strSQL := 'SELECT [Last Entry No] FROM WFSetup';
if odbcStmt.ExecDirect(strSQL) then begin
if odbcStmt.Fetch then begin
dTemp := odbcStmt.GetDataAsDouble('Last Entry No');
Result := dTemp;
dTemp := dTemp + 1;
if dTemp > 1000000 then
dTemp := 100000;
strSQL := 'UPDATE WFSetup' SET [Last Entry No] = ' + FloatToStr(dTemp);
odbcStmt2 := odbcConn.NewStatement;
if not odbcStmt2.ExecDirect(strSQL) then
...
I should point out that in my case SQL Server is being used as a back end for a Navision database, so I cannot use any SQL trigger functionality.
I know that I have to put in the locking somewhere but am unsure on how to do so.
Any help would be appreciated.
Andrew