How to insert row with ID+1

20 views
Skip to first unread message

Chris Normand

unread,
Nov 6, 2019, 3:37:49 PM11/6/19
to dbfit
 I am a long time FitNesse user, but a brand new DbFit user (and my SQL is pretty weak).

I want to find the largest number in a column, and then insert a new record with that column + 1.

First, I am uncertain how to use the stored value, is "@MAX" correct? 

!|Store Query| SELECT MAX(appl_prop_id) FROM ${PROP_TBL}|MAX|
!|Inspect Query|Select param_nm, param_val, appl_prop_id from ${PROP_TBL} where appl_prop_id=@MAX|
|param_nm|param_val|appl_prop_id|

This errors with:  org.postgresql.util.PSQLException: ERROR: column "max" does not exist.

And of course, the following fails:

!|Insert|${PROP_TBL}|
|param_nm|param_val|appl_prop_id|
|TestProperty|TestValue|@MAX+1|

with:  java.lang.NumberFormatException: For input string: "@MAX+1"

Any suggestions would be greatly appreciated.   I am trying to introduce automated testing using DbFit/FitNesse to a new project, and I have never used a relational database before (long story) :-(
Message has been deleted

Chris Normand

unread,
Nov 22, 2019, 11:04:25 AM11/22/19
to dbfit
I figured out part of it:

Prepend ">>" to the SYMBOL name to read it from the DB.
!|Query| SELECT MAX(appl_prop_id) FROM ${PROP_TBL}|
|max?|
|>>MAX_VALUE|

Prepend "_:" to the SYMBOL to use in a WHERE clause.
!|Inspect Query|Select* from ${PROP_TBL} where appl_prop_id = _:MAX_VALUE |

Prepend "<<" to the SYMBOL to use it as data in an INSERT or UPDATE.
|update|${PROP_TBL}|
|param_nm=|param_val=|appl_prop_id|
|TestName|TestVale|<<MAX_VALUE|

Mark Matten

unread,
Feb 25, 2020, 2:51:07 AM2/25/20
to dbfit
Cool. That's how I usually approach this too.
Reply all
Reply to author
Forward
0 new messages