Solved

SQL statement

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

710 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