| View previous topic :: View next topic |
| Author |
Message |
Richard Bibby Guest
|
Posted: Tue May 08, 2007 2:04 pm Post subject: Using adoCommand with a table that has an identity column |
|
|
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
|
Posted: Tue May 08, 2007 5:15 pm Post subject: Re: Using adoCommand with a table that has an identity colum |
|
|
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
|
Posted: Tue May 08, 2007 5:24 pm Post subject: Re: Using adoCommand with a table that has an identity colum |
|
|
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
|
Posted: Tue May 08, 2007 7:46 pm Post subject: Re: Using adoCommand with a table that has an identity colum |
|
|
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
|
Posted: Tue May 08, 2007 9:11 pm Post subject: Re: Using adoCommand with a table that has an identity colum |
|
|
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 |
|
 |
|