Solved

SQL statement

Posted on 1997-04-29
2
220 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
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 200 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

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

914 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

14 Experts available now in Live!

Get 1:1 Help Now