Re: @@Identity
From: Jason L James (jason_at_no-spam.dive-master.org)
Date: 08/24/04
- Next message: PeterB: "Re: @@Identity"
- Previous message: PeterB: "Re: @@Identity"
- In reply to: PeterB: "Re: @@Identity"
- Next in thread: PeterB: "Re: @@Identity"
- Reply: PeterB: "Re: @@Identity"
- Reply: PeterB: "Re: @@Identity"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 24 Aug 2004 12:21:46 GMT
Peter,
I managed to condense it to this:
Dim cmdID As String = "SELECT @@IDENTITY"
Dim cmdIdent As New SqlCeCommand(cmdID, myConn)
LastItem = CType(cmdIdent.ExecuteScalar().ToString, Int32)
The execute scalar still returns a decimal though, and not an INT,
which the the field type in my SQL CE DB.
ANy thoughts on why that might be. I would like to remove the cast
to INT32 if I can.
Thanks,
Jason.
On Tue, 24 Aug 2004 13:51:04 +0200, "PeterB" <peter@data.se> wrote:
>I don't know why it does that. What SqlCeCommand function are you using?
>
>Try using ExecuteScalar. It is the fastes way to retrieve a single data
>value.
>
>best regards,
>
> Peter
>
>
>"Jason L James" <jason@no-spam.dive-master.org> skrev i meddelandet
>news:412b25f9.756157@news.demon.co.uk...
>> Peter,
>>
>> when I executed
>>
>> SELECT @@IDENTITY FROM tblMyTable
>>
>> it returned the same @@IDENTITY value, but it did it
>> for each row in the table. That is, if there are four rows
>> in the table then the query returns 4 rows, each with the
>> same @@IDENTITY value in them?
>>
>> Any thoughts? That's why I used SELECT TOP 1 in the query
>> so that it only returned one row.
>>
>> Thanks,
>>
>> Jason.
>>
>> On Tue, 24 Aug 2004 11:00:24 +0200, "PeterB" <peter@data.se> wrote:
>>
>> >From the books online:
>> >After an INSERT, SELECT INTO, or bulk copy statement completes,
>@@IDENTITY
>> >contains the last identity value generated by the statement. If multiple
>> >rows are inserted, generating multiple identity values, @@IDENTITY
>returns
>> >the last identity value generated.
>> >
>> >So TOP 1 wouldn't be needed, you only get a single field as result..
>> >
>> > / Peter
>> >
>> >
>> >"Jason L James" <jason@nospam.divemaster.org> skrev i meddelandet
>> >news:66kli0d1pb6k37r1nrp96dvkseg0omielh@4ax.com...
>> >> Thanks, for the info. I was struglling with the conversion from
>> >> decimal to INT32 but got there in the end. I also used
>> >>
>> >> SELECT TOP 1 @@IDENTITY FROM tableName
>> >>
>> >> so that I only got 1 value, not one for every row in the table.
>> >>
>> >> Thanks,
>> >>
>> >> Jason.
>> >>
>> >> On Mon, 23 Aug 2004 15:53:32 -0500, "mgarner1980" <mgarner@kbsi.com>
>> >> wrote:
>> >>
>> >> >here's what I've used before
>> >> >
>> >> >sqlcecmd.Connection = sqlcelocaldb
>> >> >
>> >> >sqlcecmd.CommandText = materialsql
>> >> >
>> >> >sqlcecmd.ExecuteNonQuery()
>> >> >
>> >> >gstrsql = " select distinct @@identity as newmaterialid from material"
>> >> >
>> >> >sqlcecmd.CommandText = gstrsql
>> >> >
>> >> >sqlcelocaldbdr2 = sqlcecmd.ExecuteReader(CommandBehavior.SingleRow)
>> >> >
>> >> >newidentity = sqlcelocaldbdr2.GetOrdinal("newmaterialid")
>> >> >
>> >> >While sqlcelocaldbdr2.Read()
>> >> >
>> >> >newmaterialidout = sqlcelocaldbdr2.GetValue(newidentity).ToString
>> >> >
>> >> >End While
>> >> >
>> >> >
>> >> >
>> >> >
>> >> >
>> >> >"Jason L James" <jason@nospam.divemaster.org> wrote in message
>> >> >news:hk2ii095jcou1iq2bv4m7r85bp9h5loa9j@4ax.com...
>> >> >> Hi,
>> >> >>
>> >> >> does anyone know how to use @@IDENTITY with
>> >> >> SQLCE Server to return the ID of the last inserted
>> >> >> record?
>> >> >>
>> >> >> Thanks,
>> >> >>
>> >> >> Jason.
>> >> >
>> >>
>> >
>> >
>>
>
>
- Next message: PeterB: "Re: @@Identity"
- Previous message: PeterB: "Re: @@Identity"
- In reply to: PeterB: "Re: @@Identity"
- Next in thread: PeterB: "Re: @@Identity"
- Reply: PeterB: "Re: @@Identity"
- Reply: PeterB: "Re: @@Identity"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|