Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL statement

Posted on 1997-04-29
2
Medium Priority
?
235 Views
Last Modified: 2010-04-16
I have an Ms SQL table
Table1
------
ID (Counter, auto-increment) (UNIQUE)
Field1  varchar(50) (NOT UNIQUE)

In my java code, I run a statement like
executeUpdate("INSERT INTO Table1...").
I want to be abble to retreive the value "ID" if the counter
just created in SQL. What's the BEST way? (no Requery and MoveLast please...)
Thanks
0
Comment
Question by:nats042897
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1219961
Have you tried Inserting into two tables?

Make the second table contain the ID of the last element and the IP of the user that updated it. (Use this last value as an index to delete the record once you are done with it).

For instance:

executeUpdate("INSERT INTO Table1, Table2..., Table2 =Table1.ID").



0
 
LVL 1

Accepted Solution

by:
mlimotte earned 400 total points
ID: 1219962
The answer is dependant on the DBMS that you are using.

In MS SQL Server, you can "SELECT @@IDENTITY".  This will give you the last value auto-inserted into an identity column in the current session.

In Informix:  The value can be obtained with
DBINFO('sqlca.sqlerrd1')  
   LET  p_newformkey = DBINFO('sqlca.sqlerrd1');


Oracle: You can select from the SEQUENCE object first, and then insert that value into the table.


0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

722 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