Solved

SQL statement

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 51
iterator/ListIterator approach 17 28
How to execute a Python program and gather return output in Java 2 31
eclipse buid path vs tomcat lib path 10 24
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…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

821 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