Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 @@Identity

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-10-31 : 09:05:10
Maria writes "I'm building an application using Dreamweaver MX and created VBScript page to insert a record into my SQL database. How do I pass the @@Identity value to a confirmation page that echos the newly posted entry to the user? The table uses an identity column to assign to every new Work Order that goes into the database. I've looked through sample code on the internet, but because I'm using Dreamweaver, it makes it a little difficult to find the correct placement for the suggested code."

mr_mist
Grunnio

1870 Posts

Posted - 2002-10-31 : 09:09:39
quote:

Maria writes "I'm building an application using Dreamweaver MX and created VBScript page to insert a record into my SQL database. How do I pass the @@Identity value to a confirmation page that echos the newly posted entry to the user? The table uses an identity column to assign to every new Work Order that goes into the database. I've looked through sample code on the internet, but because I'm using Dreamweaver, it makes it a little difficult to find the correct placement for the suggested code."



You should just be able to do

SELECT @@identity from tablename

after you do the bit of work you're interested in.

Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-10-31 : 09:25:48
Actually, after the INSERT operation, you'd just need to use SELECT @@IDENTITY. This must be done immediately after the INSERT though; another INSERT operation will change @@IDENTITY's value regardless of whether it's the same table or a different one.

Go to Top of Page

jasper_smith
SQL Server MVP & SQLTeam MVY

846 Posts

Posted - 2002-10-31 : 16:44:07
If you are on SQL2000 then SCOPE_IDENTITY() is prefered especially if you use triggers. Have a look at @@IDENTITY and SCOPE_IDENTITY in BOL. e.g.


use tempdb
go
create table t1(colid int identity(1,1),col2 int NOT NULL)
create table t2(colid int identity(1,1),col2 int NOT NULL)
insert t2(col2) values(1)

go
create trigger t1_ins on t1
for insert
as

insert t2(col2)
select col2 from inserted

return
go


declare @id1 int,@id2 int,@id3 int,@id4 int

insert t1(col2) values(1)
select @id1=@@IDENTITY,
@id2=SCOPE_IDENTITY(),
@id3=IDENT_CURRENT('t1'),
@id4=IDENT_CURRENT('t2')
/*
actual value is 1 for t1 but @@identity returns 2 because
of the trigger inserting into t2 which has 1 row already
*/

select @id1 as '@@IDENTITY',
@id2 as 'SCOPE_IDENTITY()',
@id3 as 'IDENT_CURRENT t1',
@id4 as 'IDENT_CURRENT t2'

/*

@@IDENTITY SCOPE_IDENTITY() IDENT_CURRENT t1 IDENT_CURRENT t2
----------- ---------------- ---------------- ----------------
2 1 1 2


*/
go

drop trigger t1_ins
drop table t1
drop table t2
go




HTH
Jasper Smith
Go to Top of Page
   

- Advertisement -