the @@IDENTITY is the last identity value generated by an insert statement.
be careful though, if you do multiple inserts at one time it is the LAST
identity value.  this goes for triggers. so if an insert into table1
triggers inserts into table2 and table3, @@IDENTITY will be the identity
value from table3.  also, if no insert happened or the table does not have
an identity field it will be null.  i'm no sql expert but that is my
understanding of it.  below is a sample.  hope this helps.

DECLARE @MyNewID int

insert into Table1 (Field1) values ('hello there')
set @MyNewID = @@IDENTITY
insert into Table2 (FieldUsedToJoinToTable1) values (@MyNewID)


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On
Behalf Of Bakken, Kory
Sent: Thursday, July 26, 2001 3:07 PM
To: '[EMAIL PROTECTED]'
Subject: RE: [KCFusion] a SQL question


Can you elaborate on that @@IDENTITY statement?
Can you show me some sample usage?  I've never seen that before.

-----Original Message-----
From: Kenyon Gwillim [mailto:[EMAIL PROTECTED]]
Sent: Thursday, July 26, 2001 3:05 PM
To: [EMAIL PROTECTED]
Subject: RE: [KCFusion] a SQL question


if you mean using something like select max() then doing a select @@IDENTITY
after the first insert is probably a little safer.

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On
Behalf Of Bakken, Kory
Sent: Thursday, July 26, 2001 2:53 PM
To: '[EMAIL PROTECTED]'
Subject: RE: [KCFusion] a SQL question


Additionally, remember that if they are all related, and a primary key is
created from the first insert, then you will have to requery the table after
the insert to find the max record id for use in other insert statements.

-----Original Message-----
From: Matthew W Jones [mailto:[EMAIL PROTECTED]]
Sent: Thursday, July 26, 2001 11:56 AM
To: '[EMAIL PROTECTED]'
Subject: RE: [KCFusion] a SQL question


yes
wrap them in a transaction block, and commit if they all worked, or rollback
if any failed

-----Original Message-----
From: Adaryl Wakefield [mailto:[EMAIL PROTECTED]]
Sent: Thursday, July 26, 2001 12:01 PM
To: [EMAIL PROTECTED]
Subject: [KCFusion] a SQL question


I need to insert data into multiple tables that are related. Every example
of an insert statement I have seen only inserts info into one table. Do I
need to write 4 different INSERT statements?
A.



______________________________________________________________________
The KCFusion.org list and website is hosted by Humankind Systems, Inc.
List Archives........ http://www.mail-archive.com/cf-list@kcfusion.org
Questions, Comments or Glowing Praise.. mailto:[EMAIL PROTECTED]
To Subscribe.................... mailto:[EMAIL PROTECTED]
To Unsubscribe................ mailto:[EMAIL PROTECTED]



______________________________________________________________________
The KCFusion.org list and website is hosted by Humankind Systems, Inc.
List Archives........ http://www.mail-archive.com/cf-list@kcfusion.org
Questions, Comments or Glowing Praise.. mailto:[EMAIL PROTECTED]
To Subscribe.................... mailto:[EMAIL PROTECTED]
To Unsubscribe................ mailto:[EMAIL PROTECTED]




______________________________________________________________________
The KCFusion.org list and website is hosted by Humankind Systems, Inc.
List Archives........ http://www.mail-archive.com/cf-list@kcfusion.org
Questions, Comments or Glowing Praise.. mailto:[EMAIL PROTECTED]
To Subscribe.................... mailto:[EMAIL PROTECTED]
To Unsubscribe................ mailto:[EMAIL PROTECTED]



______________________________________________________________________
The KCFusion.org list and website is hosted by Humankind Systems, Inc.
List Archives........ http://www.mail-archive.com/cf-list@kcfusion.org
Questions, Comments or Glowing Praise.. mailto:[EMAIL PROTECTED]
To Subscribe.................... mailto:[EMAIL PROTECTED]
To Unsubscribe................ mailto:[EMAIL PROTECTED]


 
 
______________________________________________________________________
The KCFusion.org list and website is hosted by Humankind Systems, Inc.
List Archives........ http://www.mail-archive.com/cf-list@kcfusion.org
Questions, Comments or Glowing Praise.. mailto:[EMAIL PROTECTED]
To Subscribe.................... mailto:[EMAIL PROTECTED]
To Unsubscribe................ mailto:[EMAIL PROTECTED]
 

Reply via email to