BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using adoCommand with a table that has an identity column

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (ADO)
View previous topic :: View next topic  
Author Message
Richard Bibby
Guest





PostPosted: Tue May 08, 2007 2:04 pm    Post subject: Using adoCommand with a table that has an identity column Reply with quote



Hej,

I have a table in MS SQL Sever that has an identity column.

I want to insert a new row in the table and return the identity value. Is
this possible?

I was experimenting with the following:

ADODataSet1.Recordset := ADOCommand1.Execute;

I was hoping that the dataset would contain the newly created row... but I
can not get it to work.

Any ideas or other solution sto the problem?

/Richard
Back to top
Mikael Eriksson
Guest





PostPosted: Tue May 08, 2007 5:15 pm    Post subject: Re: Using adoCommand with a table that has an identity colum Reply with quote



Richard Bibby skrev:
Quote:
Hej,

I have a table in MS SQL Sever that has an identity column.

I want to insert a new row in the table and return the identity value. Is
this possible?


Hej!

After the insert you can query @@IDENTITY to get the last identity value
generated.

ADODataSet.CommandText := 'SELECT @@Identity AS COLID';

regards

/Micke
Back to top
Paul Scott
Guest





PostPosted: Tue May 08, 2007 5:24 pm    Post subject: Re: Using adoCommand with a table that has an identity colum Reply with quote



On Tue, 08 May 2007 13:15:25 +0100, Mikael Eriksson <micke314 (AT) gmail (DOT) com>
wrote:
Quote:
I have a table in MS SQL Sever that has an identity column.
I want to insert a new row in the table and return the identity
value. Is this possible?

After the insert you can query @@IDENTITY to get the last identity value
generated.
ADODataSet.CommandText := 'SELECT @@Identity AS COLID';


I see in my code that I changed to using "select scope_identity as..."
rather than
"select @@identity"

Thanks to the original poster (and google) for reminding me of the
following...


Also worth pointing out the difference between @@IDENTITY and
SCOPE_IDENTITY()

@@IDENTITY returns the last ID generated for an identity column in any
table.

SCOPE_IDENTITY returns the last ID generated in the caller scope.

The main difference is that if your statement inserts a table with an
identity column, and that table has a trigger that populates another
table with an identity column, @@IDENTITY will return the ID generated
for the table populated by the trigger, while SCOPE_IDENTITY() will
return the ID for the table specified in the insert statement.


--
Paul Scott
Information Management Systems
Macclesfield, UK.
Back to top
Kevin Frevert
Guest





PostPosted: Tue May 08, 2007 7:46 pm    Post subject: Re: Using adoCommand with a table that has an identity colum Reply with quote

Richard,

I've uploaded an example to borland.public.attachments.

Good luck,
krf

"Richard Bibby" <richard.bibby (AT) profdoc (DOT) com> wrote in message
news:46403d36$1 (AT) newsgroups (DOT) borland.com...
Quote:
Hej,

I have a table in MS SQL Sever that has an identity column.

I want to insert a new row in the table and return the identity value. Is
this possible?

I was experimenting with the following:

ADODataSet1.Recordset := ADOCommand1.Execute;

I was hoping that the dataset would contain the newly created row... but I
can not get it to work.

Any ideas or other solution sto the problem?

/Richard
Back to top
Richard Bibby
Guest





PostPosted: Tue May 08, 2007 9:11 pm    Post subject: Re: Using adoCommand with a table that has an identity colum Reply with quote

THanks for the help!

"Kevin Frevert" <kevin (AT) workdrinkingicewater (DOT) com> wrote in message
news:46408d2d$1 (AT) newsgroups (DOT) borland.com...
Quote:
Richard,

I've uploaded an example to borland.public.attachments.

Good luck,
krf

"Richard Bibby" <richard.bibby (AT) profdoc (DOT) com> wrote in message
news:46403d36$1 (AT) newsgroups (DOT) borland.com...
Hej,

I have a table in MS SQL Sever that has an identity column.

I want to insert a new row in the table and return the identity value.
Is this possible?

I was experimenting with the following:

ADODataSet1.Recordset := ADOCommand1.Execute;

I was hoping that the dataset would contain the newly created row... but
I can not get it to work.

Any ideas or other solution sto the problem?

/Richard


Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (ADO) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.