Solved

Spring JDBCTemplate - Incrementing ID column .

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
post4 challenge 28 83
seriesUp challenge 7 107
Understanding websocket example in spring 1 42
mapBully challenge 6 89
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.

759 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

18 Experts available now in Live!

Get 1:1 Help Now