I don't normally share code from our ERP system but this is how we update counters. You would have to modify to see if it is the first counter of the day and reset to 1 in your case. This is a function where you pass the counter name to it. This may not be perfect but it works well in our environment. Sorry, but this doesn't show global variable declarations or related functions (like fx_display_msg). Use it for what it is worth.
/*
This function will return a long containing the next counter number for the counter specified when the function was called.
//It will return a negative one (-1) if it loops the required number of times and cannot update.
//It will return a -2 if the select is unsuccessful.
//It will return a -3 if the commit was unsuccessful.
//It will return -4 if the connection to the database cannot be established.
*/
// Variable Declarations
boolean lb_connection_reestablished = false
long ll_counter = -1, ll_loop_counter
string ls_counter_name, ls_counter_string, ls_updated_counter_string, ls_db_error, ls_db_property
ulong lu_db_handle
uo_set_sqlca luo_set_sqlca
//Initialize variables
ls_counter_name = upper(counter_name)
ls_counter_name = upper(counter_name)
Choose Case ls_counter_name
for ll_loop_counter = 1 to 1000
//Connect
lu_db_handle=sqlca_counter.dbhandle( )
if lu_db_handle = 0 then //If not already connected
CONNECT USING sqlca_counter;
CHOOSE CASE sqlca_counter.SQLCODE
CASE -1 //error
fx_display_msg("008")//Connect To Database Manager Was Not Successful !
//display database error
ls_db_error = "SQLCode Error Number " + string(SQLCA_counter.SQLCode) + ". SQLDBCode Error Number " + string(SQLCA_counter.SQLDBCode) + ". SQLErrText = " + SQLCA_counter.SQLErrText
MessageBox( "SQL Error", ls_db_error)
return -4
CASE ELSE//connection was established
//SQL Anywhere OEM database - Set Additional Parameters for SQLCA connection
if sqlca_counter.dbms='ODBC' then
luo_set_sqlca = create uo_set_sqlca
luo_set_sqlca.uf_sql_anywhere_oem(sqlca_counter)
destroy luo_set_sqlca
end if
END CHOOSE//check for connection errors
end if
//Get the current value
select counter.counter_no
into :ls_counter_string
from counter
where counter.id = :ls_counter_name
using sqlca_counter;
Choose Case sqlca_counter.sqlcode
Case 0
Case -1
//Just in case the connection somehow got lost, try once to reestablish the connection and try again
if not lb_connection_reestablished then
lb_connection_reestablished = true
disconnect using sqlca_counter;
continue
end if
//store db error text
ls_db_error = "SQLCode Error Number " + string(sqlca_counter.SQLCode) + ". SQLDBCode Error Number " + string(sqlca_counter.SQLDBCode) + ". SQLErrText = " + sqlca_counter.SQLErrText
//set return value
rollback using sqlca_counter;
disconnect using sqlca_counter;
//display database error
MessageBox( "SQL Error", ls_db_error)
return -2
Case else
//store db error text
ls_db_error = "SQLCode Error Number " + string(sqlca_counter.SQLCode) + ". SQLDBCode Error Number " + string(sqlca_counter.SQLDBCode) + ". SQLErrText = " + sqlca_counter.SQLErrText
//set return value
rollback using sqlca_counter;
disconnect using sqlca_counter;
MessageBox( 'Error', 'Counter Was Not Found!')
return -2
end choose
// Update the counter
ll_counter = long(ls_counter_string)
//increment ls_counter_string
ls_updated_counter_string = string(ll_counter + 1)
//update - Notice the old value in the where clause
update counter
set counter_no = :ls_updated_counter_string
where counter.id = :ls_counter_name and
counter_no = :ls_counter_string using sqlca_counter;
if sqlca_counter.sqlcode = 0 and sqlca_counter.sqlnrows = 1 then
commit using sqlca_counter;
//check for commit errors
if sqlca_counter.sqlcode = 0 then
//Commit was successful
return ll_counter
end if
//Commit was not successful
if ll_loop_counter < 1000 then
rollback using sqlca_counter;
continue
end if
ls_db_error = "SQLCode Error Number " + string(sqlca_counter.SQLCode) + ". SQLDBCode Error Number " + string(sqlca_counter.SQLDBCode) + ". SQLErrText = " + sqlca_counter.SQLErrText
rollback using sqlca_counter;
//only display error, if this is the last time we are running the loop
disconnect using sqlca_counter;
MessageBox( "SQL Error", ls_db_error)
return -3
else
ls_db_error = "SQLCode Error Number " + string(sqlca_counter.SQLCode) + ". SQLDBCode Error Number " + string(sqlca_counter.SQLDBCode) + ". SQLErrText = " + sqlca_counter.SQLErrText
rollback using sqlca_counter;
if ll_loop_counter < 1000 then
continue
end if
disconnect using sqlca_counter;
//display database error
MessageBox( "SQL Error", ls_db_error)
return -1
end if
Next//loop to try to get the counter
However, sometimes there are tracking requirements, so the truncate method is not suitable.
And sometimes using a trigger is not an option, for whatever reason - like the DBMS does not support triggers.