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 2005 Forums
 Transact-SQL (2005)
 Using data after insert into table

Author  Topic 

minhle
Starting Member

4 Posts

Posted - 2011-11-03 : 18:39:25
dear all

I want to use data (Autonumber) after insert into table but I have no way. Exam I have table abcd(ID, name) where ID is autonumber.
I insert by this command
Insert into abcd(name) values ('my text')
after inserted, I want to use the value of autonumer ID. How can I do?

I write by the old way but cannot run:

rs.addnew
rs("name") = "my text"
rs.update
response.write rs("ID")

Who know the way and help me? I am new in SQL ...

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-11-03 : 18:43:19
You can save the auto-generated identity value by calling SCOPE_IDENTITY() function.

Here's an example in T-SQL:

declare @i int

insert into t1 (c2 varchar(5))
values ('test')

set @i = SCOPE_IDENTITY()


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

minhle
Starting Member

4 Posts

Posted - 2011-11-03 : 18:51:16
quote:
Originally posted by tkizer

You can save the auto-generated identity value by calling SCOPE_IDENTITY() function.

Here's an example in T-SQL:

declare @i int

insert into t1 (c2 varchar(5))
values ('test')

set @i = SCOPE_IDENTITY()


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog



Can you change to ASP?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-11-03 : 18:54:36
I can't. I'm a SQL DBA only.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

minhle
Starting Member

4 Posts

Posted - 2011-11-03 : 21:22:52
Anybody can help me?
Go to Top of Page
   

- Advertisement -