0

Does FitNesse/dbFit support inserting into a table with a single identity column and then returning that value into a variable?

For example:

create table TestTable ( ID int identity(1,1) )

!|Insert|TestTable|
|ID?              |
|>>TestID         |

results in the following exception:

System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near ')'.

Is there another way to accomplish this?

sduplooy
  • 12,740
  • 9
  • 39
  • 59
  • Also, there are more than one table for which this needs to happen and so I cannot have multiple 'set identity_insert on' statements. – sduplooy Sep 12 '12 at 06:56

1 Answers1

0

Your problem is not with Fitnesse, but with the SQL engine. If you try the same query in SQL Server Management Studio you will receive the same error. Thus, given the restriction that you cannot use set identity_insert on the question you really need to ask is how to insert a record with no insertable fields in SQL Server independent of Fitnesse? This StackOverflow post provides a simple answer:

INSERT INTO #TempTable DEFAULT VALUES

Having that in hand, now the task is to map this to Fitnesse. Here is one solution:

!|execute|CREATE TABLE #TestTable ( ID int identity(1,1) )|

!|execute|INSERT INTO #TestTable DEFAULT VALUES|

!|query|SELECT TOP 1 @@IDENTITY [ID] FROM #TestTable|
|ID?                                                |
|>>TestID                                           |

Fitnesse's INSERT command does not support the default construct, so you must switch to the more general EXECUTE command to do the insert. That, however, does not return results so you cannot glean the auto-inserted ID value directly. The final query gives you one way to grab the just-inserted identity value.

Community
  • 1
  • 1
Michael Sorens
  • 32,325
  • 20
  • 111
  • 165