views:

26

answers:

1

Hi, I have created a table in MSSQL having following structure -

**Table Name - TestTable**
id (int) (Primary key)
name (varchar) 
owner (varchar)

I have given Identity specification for column 'id' with auto increment with 1 for each new row. When I created iBatis artifacts for TestTable, the insert function is getting generated, in the DAO, with following signature -

 void insert(TestTable record); 

I want the insert function to return the newly generated 'id' for that row, instead of void.

How to achieve this?

+1  A: 

In your ibatis SqlMap file, add the following:

<insert id="myInsertStatement">
    INSERT INTO TestTable (name, owner)
    VALUES (#name#, #owner#)
    <selectKey keyProperty="id" resultClass="int">
         SELECT SCOPE_IDENTITY()
    </selectKey>
</insert>

Or you could replace Select Scope_identity with another SQL query designed to return the last identity column inserted. The Scope_identity is specific to MS SQL (as requested in the question).

David-Zazeski
Thanks a lot :)
psvm