Solved

Spring JDBCTemplate - Incrementing ID column .

Posted on 2013-11-15
3
533 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 500 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
matchUp  challenge 9 105
strCopies  challenge 17 87
strDist challenge 35 103
eclipse apache tomcat admin console 52 95
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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.

786 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