Solved

Spring JDBCTemplate - Incrementing ID column .

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using object mapper in spring web application 8 54
stringclean challenge 26 70
firstChar challenge 13 110
servlet example issue 6 31
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 second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now