RE: Getting Identity Column value

From: Alejandro Mesa (AlejandroMesa_at_discussions.microsoft.com)
Date: 09/17/04


Date: Fri, 17 Sep 2004 05:49:11 -0700

Yes you can.

- To get last inserted in current scope (a stored procedure, trigger,
function, or batch) use function SCOPE_IDENTITY().

- To get the last not limited to a specific scope use @@IDENTITY

- To get the inserted into a specific table, no matter scope or session, use
function IDENT_CURRENT

Example:

create table tableA(colA int not null identity(1, 1))
create table tableB(colA int not null identity(100, 1))
go

create trigger tr_tableA_I on tableA
for insert
as
insert into tableB default values
go

insert into tableA default values

-- print the last identity inserted
-- in this case, the last value inserted in tableB because tableA has an
after insert trigger that insert into tableB
print @@identity

-- the last inserted in this batch or objects visible in this batch
print scope_identity()

-- last inserted into a specific table
print ident_current(tableA)
print ident_current(tableB)
go

drop table tableA, tableB
go

AMB

"Satya" wrote:

> Hai all,
>
> Is there any way to get the identiy col value that would be generated on a
> table if i inserted a row?
>
> Or put it another way can i get the last identity col value generated on a
> table?
>
> TIA
>
> Satya



Relevant Pages

  • Re: Global Cursor
    ... Specifies that the scope of the cursor is local to the batch, ... or trigger in which the cursor was created. ...
    (microsoft.public.sqlserver.clients)
  • Re: What guns would you most love receiving as a birthday gift?
    ... Heavy barrel .220 swift, accurized with a smooth 3# trigger, bipod, and a ... clear, bright, 6-24 scope. ... Elk, moose, bears, and hogs. ...
    (rec.guns)
  • Re: HWS Missing Tasks
    ... Adding a atomic scope arround my send port has resolved the issue ... Hennie E ... > Tracking data is delivered to the Hws tables in batches which occur in two ... > of such a scope will force a batch point. ...
    (microsoft.public.biztalk.general)
  • Re: (Using Lab-Volt System) Have the books, how do I get a trace on my 465 Tektronix oscilloscop
    ... any scope will show 60Hz sort of sinewave ... Given a trace, it will merely allow ... won't get a display, and if the sweep is really fast, you may ... each time it receives a trigger voltage of suitable level. ...
    (sci.electronics.basics)
  • Re: Opinions on Oscilloscopes
    ... >> At least that is how Tektronix specs their scopes. ... >> Why are you going to put 250VAC into the TRIGGER input? ... > need to put a scope on both sides at some stage. ...
    (sci.electronics.equipment)