Ask a Question related to ASP.NET General, Design and Development.
-
Joël #1
Re: retrieving identity
It's better to use SCOPE_INDENTITY( )
@@IDENTITY can give a bad answer in some situations.
"Kevin Spencer" <kevin@SPAMMERSSUCKtakempis.com> a écrit dans le message de
news:uYJX8$0ODHA.3700@tk2msftngp13.phx.gbl...value> Use a Stored Procedure. It performs the INSERT and then does a SELECT
> @@IDENTITY AS 'Identity'. Execute the Stored Procedure using a Command
> object, and returning the result to a DataReader.
>
> HTH,
>
> Kevin Spencer
> Microsoft FrontPage MVP
> Internet Developer
> [url]http://www.takempis.com[/url]
> Big things are made up of
> lots of Little things.
>
> "belgie" <belgie@hns.com> wrote in message
> news:%23LNhn80ODHA.2788@TK2MSFTNGP10.phx.gbl...> > I am using a SQLCommand to insert a row in a table which has an identity
> > field as the primary key.
> >
> > I haven't come up with a good method of retrieving the new identity> and> > after the insert is complete. The best I have found is to do the insert>> > then do a select using the values just inserted to retrieve the identity
> > value.
> >
> > Is there a more efficient way to do this?
> >
> > Thanks!
> > Bill
> >
> >
> >
>
Joël Guest
-
retrieving last record inserted using @@IDENTITY - ASPJScript
Im triyng to retrieve the last record inserted usnig the "Insert" Server Behavior, in a table that have an auto-incrementig id column. Im trying... -
ASP.NET Identity
Any help? Thanks! rovver2001@hotmail.com (CN) wrote in message news:<d3fdd940.0401040934.cef803c@posting.google.com>... -
User.Identity.Name
Hi, I am using Forms authentication -in which - I am trying to use User.Identity.Name, i am getting the user name in this field even after i... -
@@IDENTITY is 0 ?
I've hit a problem with INSERT and @@IDENTITY for Access that is driving me up the wall. I'm using the OLEDB Jet4.0 in my connection string, so... -
@@IDENTITY in Informix
Hello, Does anyone know how to find out the primary key value of last inserted record? In SQL Server this would be done as follows: INSERT... -
Duncan Godwin #2
Re: retrieving identity
Essentially is the same solution as previously mentioned, use an output
parameter instead.
_com.CommandText = @"INSERT INTO Emp(Name) VALUES(@Name);
DECLARE @ID int
SET @ID = @@Identity
"; // the ; is important after the first query
_com.Parameters.Add("@Name", SqlVarChar.VarChar, 50);
_com.Parameters.Add("@ID", SqlVarChar.Int);
_com.Parameters["@ID"].Direction = ParameterDirection.Output.
_com.Parameters["@ID"].Value = "Me";
nRowsAffected = _com.ExecuteNonQuery();
int ident = (int)_com.Parameters["@ID"].Value;
Something like that should work, but I haven't tested it, it's the same
principle when you
use a stored procedure.
Duncan
"William F. Robertson, Jr." <wfrobertson@kpmg.com> wrote in message
news:#Rxtql1ODHA.2316@TK2MSFTNGP11.phx.gbl...added> Well then maybe someone could help me with mine.
>
> I have an insert query that needs to be run, the RowsAffected is returned
> from the ExecuteNonQuery(), but I also need to get the identity of thethe> row.
>
> Currently I am using
>
> _com.CommandText = <insert query>;
> nRowsAffected = _com.ExecuteNonQuery();
> _com.CommandText = "Select @@Identity";
> nIdentity = _com.ExecuteScalar();
>
> I tried changing the Select @@Identity to scope_Identity() but that didn't
> work since it is two different commands, but the @@Identity will returnbecause> one I am looking for (as long as there are no triggers or anything)> it is in the same session (Connection).
>
> bill
>
>
>
> "Joël" <jdescombes@netcourrier.com> wrote in message
> news:3ef9faee$0$17295$79c14f64@nan-newsreader-03.noos.net...> de> > It's better to use SCOPE_INDENTITY( )
> > @@IDENTITY can give a bad answer in some situations.
> >
> > "Kevin Spencer" <kevin@SPAMMERSSUCKtakempis.com> a écrit dans le message> identity> > news:uYJX8$0ODHA.3700@tk2msftngp13.phx.gbl...> > > Use a Stored Procedure. It performs the INSERT and then does a SELECT
> > > @@IDENTITY AS 'Identity'. Execute the Stored Procedure using a Command
> > > object, and returning the result to a DataReader.
> > >
> > > HTH,
> > >
> > > Kevin Spencer
> > > Microsoft FrontPage MVP
> > > Internet Developer
> > > [url]http://www.takempis.com[/url]
> > > Big things are made up of
> > > lots of Little things.
> > >
> > > "belgie" <belgie@hns.com> wrote in message
> > > news:%23LNhn80ODHA.2788@TK2MSFTNGP10.phx.gbl...
> > > > I am using a SQLCommand to insert a row in a table which has an> insert> > value> > > > field as the primary key.
> > > >
> > > > I haven't come up with a good method of retrieving the new identity> > > > after the insert is complete. The best I have found is to do the> identity> > > and
> > > > then do a select using the values just inserted to retrieve the>> >> > > > value.
> > > >
> > > > Is there a more efficient way to do this?
> > > >
> > > > Thanks!
> > > > Bill
> > > >
> > > >
> > > >
> > >
> > >
> >
>
Duncan Godwin Guest
-
Kevin Spencer #3
Re: retrieving identity
The reason I recommended using a Stored Procedure is that you need to get
the @@IDENTITY value immediately after doing the INSERT. Doing it in 2
separate operations will yield untrustworthy results. How many other INSERTS
might be performed prior to your second query being executed on your live
site?
HTH,
Kevin Spencer
Microsoft FrontPage MVP
Internet Developer
[url]http://www.takempis.com[/url]
Big things are made up of
lots of Little things.
"William F. Robertson, Jr." <wfrobertson@kpmg.com> wrote in message
news:%23Rxtql1ODHA.2316@TK2MSFTNGP11.phx.gbl...added> Well then maybe someone could help me with mine.
>
> I have an insert query that needs to be run, the RowsAffected is returned
> from the ExecuteNonQuery(), but I also need to get the identity of thethe> row.
>
> Currently I am using
>
> _com.CommandText = <insert query>;
> nRowsAffected = _com.ExecuteNonQuery();
> _com.CommandText = "Select @@Identity";
> nIdentity = _com.ExecuteScalar();
>
> I tried changing the Select @@Identity to scope_Identity() but that didn't
> work since it is two different commands, but the @@Identity will returnbecause> one I am looking for (as long as there are no triggers or anything)> it is in the same session (Connection).
>
> bill
>
>
>
> "Joël" <jdescombes@netcourrier.com> wrote in message
> news:3ef9faee$0$17295$79c14f64@nan-newsreader-03.noos.net...> de> > It's better to use SCOPE_INDENTITY( )
> > @@IDENTITY can give a bad answer in some situations.
> >
> > "Kevin Spencer" <kevin@SPAMMERSSUCKtakempis.com> a écrit dans le message> identity> > news:uYJX8$0ODHA.3700@tk2msftngp13.phx.gbl...> > > Use a Stored Procedure. It performs the INSERT and then does a SELECT
> > > @@IDENTITY AS 'Identity'. Execute the Stored Procedure using a Command
> > > object, and returning the result to a DataReader.
> > >
> > > HTH,
> > >
> > > Kevin Spencer
> > > Microsoft FrontPage MVP
> > > Internet Developer
> > > [url]http://www.takempis.com[/url]
> > > Big things are made up of
> > > lots of Little things.
> > >
> > > "belgie" <belgie@hns.com> wrote in message
> > > news:%23LNhn80ODHA.2788@TK2MSFTNGP10.phx.gbl...
> > > > I am using a SQLCommand to insert a row in a table which has an> insert> > value> > > > field as the primary key.
> > > >
> > > > I haven't come up with a good method of retrieving the new identity> > > > after the insert is complete. The best I have found is to do the> identity> > > and
> > > > then do a select using the values just inserted to retrieve the>> >> > > > value.
> > > >
> > > > Is there a more efficient way to do this?
> > > >
> > > > Thanks!
> > > > Bill
> > > >
> > > >
> > > >
> > >
> > >
> >
>
Kevin Spencer Guest
-
William F. Robertson, Jr. #4
Re: retrieving identity
The thing is I have a InsertCommand object for my developers to use that
will insert for them, and they can set a bool to determine if the identity
should be captured. So when they call the Execute method, it will run their
query and grab the identity if it is applictiable.
Any other suggestions would be highly appreciated.
Thanks,
bill
"Kevin Spencer" <kevin@SPAMMERSSUCKtakempis.com> wrote in message
news:OfiSqt9ODHA.1072@TK2MSFTNGP10.phx.gbl...INSERTS> The reason I recommended using a Stored Procedure is that you need to get
> the @@IDENTITY value immediately after doing the INSERT. Doing it in 2
> separate operations will yield untrustworthy results. How many otherreturned> might be performed prior to your second query being executed on your live
> site?
>
> HTH,
>
> Kevin Spencer
> Microsoft FrontPage MVP
> Internet Developer
> [url]http://www.takempis.com[/url]
> Big things are made up of
> lots of Little things.
>
> "William F. Robertson, Jr." <wfrobertson@kpmg.com> wrote in message
> news:%23Rxtql1ODHA.2316@TK2MSFTNGP11.phx.gbl...> > Well then maybe someone could help me with mine.
> >
> > I have an insert query that needs to be run, the RowsAffected isdidn't> added> > from the ExecuteNonQuery(), but I also need to get the identity of the> > row.
> >
> > Currently I am using
> >
> > _com.CommandText = <insert query>;
> > nRowsAffected = _com.ExecuteNonQuery();
> > _com.CommandText = "Select @@Identity";
> > nIdentity = _com.ExecuteScalar();
> >
> > I tried changing the Select @@Identity to scope_Identity() but thatmessage> the> > work since it is two different commands, but the @@Identity will return> because> > one I am looking for (as long as there are no triggers or anything)> > it is in the same session (Connection).
> >
> > bill
> >
> >
> >
> > "Joël" <jdescombes@netcourrier.com> wrote in message
> > news:3ef9faee$0$17295$79c14f64@nan-newsreader-03.noos.net...> > > It's better to use SCOPE_INDENTITY( )
> > > @@IDENTITY can give a bad answer in some situations.
> > >
> > > "Kevin Spencer" <kevin@SPAMMERSSUCKtakempis.com> a écrit dans leSELECT> > de> > > news:uYJX8$0ODHA.3700@tk2msftngp13.phx.gbl...
> > > > Use a Stored Procedure. It performs the INSERT and then does aCommand> > > > @@IDENTITY AS 'Identity'. Execute the Stored Procedure using aidentity> > identity> > > > object, and returning the result to a DataReader.
> > > >
> > > > HTH,
> > > >
> > > > Kevin Spencer
> > > > Microsoft FrontPage MVP
> > > > Internet Developer
> > > > [url]http://www.takempis.com[/url]
> > > > Big things are made up of
> > > > lots of Little things.
> > > >
> > > > "belgie" <belgie@hns.com> wrote in message
> > > > news:%23LNhn80ODHA.2788@TK2MSFTNGP10.phx.gbl...
> > > > > I am using a SQLCommand to insert a row in a table which has an> > > > > field as the primary key.
> > > > >
> > > > > I haven't come up with a good method of retrieving the new>> > insert> > > value
> > > > > after the insert is complete. The best I have found is to do the> > identity> > > > and
> > > > > then do a select using the values just inserted to retrieve the> >> > > > > value.
> > > > >
> > > > > Is there a more efficient way to do this?
> > > > >
> > > > > Thanks!
> > > > > Bill
> > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
>
William F. Robertson, Jr. Guest
-
William F. Robertson, Jr. #5
Re: retrieving identity
Duncan,
You solution, aside from your predestined syntax errors, worked wonderfully.
Thanks,
bill
"William F. Robertson, Jr." <wfrobertson@kpmg.com> wrote in message
news:u4gRfUCPDHA.3408@tk2msftngp13.phx.gbl...their> The thing is I have a InsertCommand object for my developers to use that
> will insert for them, and they can set a bool to determine if the identity
> should be captured. So when they call the Execute method, it will runget> query and grab the identity if it is applictiable.
>
> Any other suggestions would be highly appreciated.
>
> Thanks,
>
> bill
>
>
>
> "Kevin Spencer" <kevin@SPAMMERSSUCKtakempis.com> wrote in message
> news:OfiSqt9ODHA.1072@TK2MSFTNGP10.phx.gbl...> > The reason I recommended using a Stored Procedure is that you need tolive> INSERTS> > the @@IDENTITY value immediately after doing the INSERT. Doing it in 2
> > separate operations will yield untrustworthy results. How many other> > might be performed prior to your second query being executed on yourreturn> returned> > site?
> >
> > HTH,
> >
> > Kevin Spencer
> > Microsoft FrontPage MVP
> > Internet Developer
> > [url]http://www.takempis.com[/url]
> > Big things are made up of
> > lots of Little things.
> >
> > "William F. Robertson, Jr." <wfrobertson@kpmg.com> wrote in message
> > news:%23Rxtql1ODHA.2316@TK2MSFTNGP11.phx.gbl...> > > Well then maybe someone could help me with mine.
> > >
> > > I have an insert query that needs to be run, the RowsAffected is> didn't> > added> > > from the ExecuteNonQuery(), but I also need to get the identity of the> > > row.
> > >
> > > Currently I am using
> > >
> > > _com.CommandText = <insert query>;
> > > nRowsAffected = _com.ExecuteNonQuery();
> > > _com.CommandText = "Select @@Identity";
> > > nIdentity = _com.ExecuteScalar();
> > >
> > > I tried changing the Select @@Identity to scope_Identity() but that> > > work since it is two different commands, but the @@Identity willthe> message> > the> > because> > > one I am looking for (as long as there are no triggers or anything)> > > it is in the same session (Connection).
> > >
> > > bill
> > >
> > >
> > >
> > > "Joël" <jdescombes@netcourrier.com> wrote in message
> > > news:3ef9faee$0$17295$79c14f64@nan-newsreader-03.noos.net...
> > > > It's better to use SCOPE_INDENTITY( )
> > > > @@IDENTITY can give a bad answer in some situations.
> > > >
> > > > "Kevin Spencer" <kevin@SPAMMERSSUCKtakempis.com> a écrit dans le> SELECT> > > de
> > > > news:uYJX8$0ODHA.3700@tk2msftngp13.phx.gbl...
> > > > > Use a Stored Procedure. It performs the INSERT and then does a> Command> > > > > @@IDENTITY AS 'Identity'. Execute the Stored Procedure using a> identity> > > > > object, and returning the result to a DataReader.
> > > > >
> > > > > HTH,
> > > > >
> > > > > Kevin Spencer
> > > > > Microsoft FrontPage MVP
> > > > > Internet Developer
> > > > > [url]http://www.takempis.com[/url]
> > > > > Big things are made up of
> > > > > lots of Little things.
> > > > >
> > > > > "belgie" <belgie@hns.com> wrote in message
> > > > > news:%23LNhn80ODHA.2788@TK2MSFTNGP10.phx.gbl...
> > > > > > I am using a SQLCommand to insert a row in a table which has an
> > > identity
> > > > > > field as the primary key.
> > > > > >
> > > > > > I haven't come up with a good method of retrieving the new> > > > value
> > > > > > after the insert is complete. The best I have found is to do>> >> > > insert
> > > > > and
> > > > > > then do a select using the values just inserted to retrieve the
> > > identity
> > > > > > value.
> > > > > >
> > > > > > Is there a more efficient way to do this?
> > > > > >
> > > > > > Thanks!
> > > > > > Bill
> > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
>
William F. Robertson, Jr. Guest



Reply With Quote

