Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


Return identity id from insert_SQLFormat this article printer-friendly!Bookmark function is only available for registered users!
identity column in sql table (tested with sqlserver 7, ado, d5, asp)
Product:
Delphi all versions
Category:
Database-SQL
Skill Level:
Scoring:
Last Update:
02/01/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet [ SET NOCOUNT statement ]
Times Scored:
4
Visits:
5569
Uploader: chiefClaudio Hira
Company: -
Reference: N/A
 
Question/Problem/Abstract:
How can i obtain the value of the identity column in a table, when inserting a record with a INSERT SQL statement.
Answer:



Found the following note when surfing several sql-dba website forums:

-----------note:
You can use the SET NOCOUNT statement. SET NOCOUNT ON will prevent SQL Server from telling you how many rows each statement affected. SET NOCOUNT OFF will return SQL Server back to it's default setting. The variable @@ROWCOUNT will always contain the number of rows affected by the previous statement regardless of the setting of NOCOUNT
-----------end note;

do use this statement to obtain the value of the inserted id:

whe have a table called TheTable
with fields:
- Field_ID (identity column)
- Field001 (varchar)
- Field002 (int)

--------start SQL script:
Set Nocount on
Insert TheTable (Field001, Field002)
VALUES ('ABC', 1)
select IdentityInsert=@@identity
set nocount off
----------end SQL script;

example usage:

I have an order table with the primary order data, and a related table called orderdetails with the orderdetails per primary order data.
The primary order table contains a key, field OrderID, of type identity-column autoincrement start at 0 and increment with 1. The table orderdetails contains the orderId as foreign key.

Case asp e-business website:
The visitor wants to checkout the order wich is composed. Now fire the insert sql to insert a record in the primary order table, using the script in this article to obtain the order id.
With the obtained order id fire the several sql_inserts into the the order details table.


enjoy...

regards
Claudio Hira (Quality-Solutions)





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
if use interbase with StoreProcedure,Advise Please.
    nakawut srisukjaisamran (Jan 9 2003 10:23AM)

If i use Interbase DB with StorePrecedure and not have generator,trigger,
How i can get Return Value of identity from insert_sql(StoreProcedure)?
Or, How to get identity id from other way? (Interbase 6x)

I 'm junior database programm.Please help.
PS. The SQL Server have @@identity function, it's convenient!
Respond

RE: if use interbase with StoreProcedure,Advise Please.
Jan Van Riebeeck (Jul 30 2003 10:28AM)

nakawut,

The way I do it is I add a field in the table Varchar(20), and I insert a GUID into this field, and after insert I do a query on the table Using the GUID in the where clause, which returns the Record just inserted. I found this method to be a lot saver then using getmax.

Regards
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
A. B. Talal
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)