Providing freelance creative services since 1993

Incremental Numbers in CF...

As I was developing an app for a client that included an online ordering system I found I had a need to increase the purchase order number by one once the order was validated and inserted into the db.  I searched for a solution to easily incrementing the numbers and found very few ideas. So after some experimenting this is what I used.



<!---First lock the transaction so as not it's not overwritten by another transaction. More on cftransaction --->
<cftransaction>

<!---Get the last number entered and increase it by one--->
<!--- Start - Get Last Number --->
<cfquery name="yourQuery" datasource="yourDSN">
SELECT MAX(ColumnName) as LastNumber
FROM yourTableName</cfquery>
<cfset NewNumber = yourQuery.LastNumber + 1>
<!--- End - Get Last Number --->

Then insert your new entry before ending the cftransaction using the NewNumber as a reference.

</cftransaction>

It's nothing fancy, in fact probably a bit crude. But it works.

 
0 Comments



Leave a comment.



Close
E-mail It