Re: @@Identity

From: William \(Bill\) Vaughn (billvaRemoveThis_at_nwlink.com)
Date: 09/16/04


Date: Thu, 16 Sep 2004 16:09:51 -0700

VB.NET (ADO.NET) knows nothing about @@Identity. This SQL Server global
function is used in a query after ADO.NET executes the INSERT via the Update
method. You have to add this SELECT to the INSERT (and UPDATE) Commands. The
DataAdapter configuration wizard can do this for you if you ask nicely. I
wrote an article on handling identity issues that might be useful.
See www.betav.com/articles.htm (MSDN).

-- 
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
"Scott M." <NoSpam@NoSpam.com> wrote in message 
news:eFFPY9DnEHA.1248@TK2MSFTNGP09.phx.gbl...
> My question is:  do I need to do something to establish the Identity 
> column in the first place if the data is coming from an existing database 
> that has a primary key already set in the db?
>
> How does VB.NET know which column I'm talking about when I use @@Identity?
>
>
> "Mary Chipman" <mchip@online.microsoft.com> wrote in message 
> news:m93jk09dnd46jfv1hrjpvcv19i4q1m3gbs@4ax.com...
>> Unless I'm misunderstanding your use of syntax, you are confusing the
>> @@Identity function with a column with the identity property set.
>> @@identity is used in the case where you have just inserted a new row
>> in a table that has an identity column defined and you want to
>> retrieve the new identity value. All of the "@@" globals are functions
>> which return some kind of information from the server. So when
>> selecting from a table with an identity column you simply refer to it
>> by its column name, like any other column. If you are updating the
>> row, omit the identity column from the update statement (except in the
>> WHERE clause, where you refer to it by its column name). For more
>> information on identity columns and @@identity, see SQL Books Online.
>>
>> --Mary
>>
>> On Thu, 16 Sep 2004 00:09:37 -0500, "Scott M." <NoSpam@NoSpam.com>
>> wrote:
>>
>>>Thanks for your reply.  I am using VB.NET, not C#, so I'm not sure what 
>>>your
>>>code snippet is designed to do.  If I have a simple SELECT like "SELECT *
>>>FROM foo" and "foo" already contains a primary key in the db, would that
>>>field automatically be the @@Identity field when it is brought into my
>>>DataSet?  If not, what would I do to mark the correct column as the 
>>>identity
>>>field?
>>>
>>>Thanks.
>>>
>>>
>>>
>>>"Gaurav Vaish" <mDaOsNtOeTrSgPaAuMrPaLvS.nospam@nospam.lycos.com> wrote 
>>>in
>>>message news:%23YGCuk6mEHA.644@tk2msftngp13.phx.gbl...
>>>>> How do I set the @@Identity parameter for data that has been read in 
>>>>> from
>>>> a
>>>>> database?  I have read in data via a command (SELECT statement) and am
>>>>> trying to build a good UPDATE statement and want to use the @@Identity
>>>>> field.
>>>>
>>>>
>>>> Hi,
>>>>
>>>>    Directly use the value of the identity field. I assume that you know
>>>> the
>>>> name of the field. For example, if you have a table like:
>>>>
>>>>    t_sample {
>>>>        id as autonumber,
>>>>        first as varchar(50),
>>>>        second as int
>>>>    }
>>>>
>>>>    Doing a select that includes "id" as a field will give you the
>>>> @@Identity (the autonumber). You can use this (columns["id"]) value 
>>>> during
>>>> your update command.
>>>>
>>>>    Does this answer your question? Or, then, can you provide your 
>>>> SELECT
>>>> and UPDATE statements? That may things more visible!
>>>>
>>>> -- 
>>>>
>>>> Happy Hacking,
>>>> Gaurav Vaish
>>>> http://www.mastergaurav.org
>>>> -----------------------------------
>>>>
>>>>
>>>
>>
>
> 


Relevant Pages

  • Re: @@Identity
    ... > How do I set the @@Identity parameter for data that has been read in from ... I have read in data via a command and am ... > trying to build a good UPDATE statement and want to use the @@Identity ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Table doesnt update - What am I doing wrong?
    ... You can get the exact syntax of the UPDATE statement ... >> Your DataAdapter.Update command needs to have an UpdateCommand property ... >> for DataAdapter.Update to execute. ... >>> End Sub ...
    (microsoft.public.dotnet.languages.vb)
  • Re: LIKE and IN expressions in parameter queries
    ... > Rob Oldfield wrote: ... >> parameter and just use that as a LIKE/IN comparison with my SQL data. ... > UPDATE statement. ...
    (microsoft.public.dotnet.general)
  • Re: DataAdapter not updating all columns of modified row via. CommandBuilder
    ... Is this CB being called each time you execute Update? ... account for what you're seeing. ... > I'm feverishly attempting to use a custom "Command Builder" of sorts. ... Since the update statement ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Command sent to Access Database using OleDbDataAdapter
    ... you should get rid of it in your code. ... "Syntax error in UPDATE statement". ... an errorcode 3000. ...
    (microsoft.public.dotnet.framework.adonet)