?
Solved

Spring JDBCTemplate - Incrementing ID column .

Posted on 2013-11-15
3
Medium Priority
?
698 Views
Last Modified: 2014-01-21
All,

I have an id column which is not set as Auto increment . We have to manaully increment it everytime we insert a record like this (select max([ERRORKEY]) from ERROR_LOG_TEMP])+ 1

Is there a way I can auto generate it during every insert using Spring JDBC template ??
0
Comment
Question by:FranklinRaj22
  • 2
3 Comments
 
LVL 14

Expert Comment

by:jb1dev
ID: 39655049
Given a table foo with columns a, b, c, id (where "id" is the column to increment) can you not do something like:

                    INSERT INTO foo 
                       (a, b, c, id) 
                     VALUES (?, ?, ?, 
                       (SELECT IFNULL(MAX(id), 0) + 1 FROM foo) 
                     ) 

Open in new window

0
 
LVL 14

Accepted Solution

by:
jb1dev earned 1500 total points
ID: 39655053
That IFNULL() I think is specific to MySQL and we use it to handle the case of the first insert into the table where there are zero rows and hence the nested select statement would return null, we need to coalesce that null value to a 0.
For other database equivalents of that function, see:
http://www.w3schools.com/sql/sql_isnull.asp
0
 

Author Comment

by:FranklinRaj22
ID: 39667648
ok
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question