Hi,
I insert a new record with a command:
private SqlCommand comInsert;
comInsert = conDataBase.CreateCommand();
comInsert.CommandType = CommandType.Text;
comInsert.CommandText = "Insert Into MyTable ( Col1, Col2) Values(1,2)";
comInsert.ExecuteNonQuery();
Col3 is an Identity column which autoincrements. How can I get the value of
the inserted record
immediately after, in an multi-user environment, on a MS SQL DB?
Thanks,
Doru 8 1900
Hello Doru,
Call SELECT @@idetity
DR> I insert a new record with a command:
DR>
DR> private SqlCommand comInsert;
DR>
DR> comInsert = conDataBase.CreateCommand();
DR>
DR> comInsert.CommandType = CommandType.Text;
DR>
DR> comInsert.CommandText = "Insert Into MyTable ( Col1, Col2)
DR> Values(1,2)";
DR>
DR> comInsert.ExecuteNonQuery();
DR>
DR> Col3 is an Identity column which autoincrements. How can I get the
DR> value of the inserted record
DR>
DR> immediately after, in an multi-user environment, on a MS SQL DB?
DR>
DR> Thanks,
DR>
DR> Doru
DR>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour
"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Thanks Michael for the reply.
Would that return only my @@identity or it might return some other user's
value if that user got a new value inserted immediately after me?
"Michael Nemtsev" <ne*****@msn.com> wrote in message
news:9c**************************@msnews.microsoft .com... Hello Doru,
Call SELECT @@idetity
DR> I insert a new record with a command: DR> DR> private SqlCommand comInsert; DR> DR> comInsert = conDataBase.CreateCommand(); DR> DR> comInsert.CommandType = CommandType.Text; DR> DR> comInsert.CommandText = "Insert Into MyTable ( Col1, Col2) DR> Values(1,2)"; DR> DR> comInsert.ExecuteNonQuery(); DR> DR> Col3 is an Identity column which autoincrements. How can I get the DR> value of the inserted record DR> DR> immediately after, in an multi-user environment, on a MS SQL DB? DR> DR> Thanks, DR> DR> Doru DR> --- WBR, Michael Nemtsev :: blog: http://spaces.msn.com/laflour
"At times one remains faithful to a cause only because its opponents do not cease to be insipid." (c) Friedrich Nietzsche
Hi,
Also note that you have to change from ExecuteNonQuery to ExecuteScalar
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Michael Nemtsev" <ne*****@msn.com> wrote in message
news:9c**************************@msnews.microsoft .com... Hello Doru,
Call SELECT @@idetity
DR> I insert a new record with a command: DR> DR> private SqlCommand comInsert; DR> DR> comInsert = conDataBase.CreateCommand(); DR> DR> comInsert.CommandType = CommandType.Text; DR> DR> comInsert.CommandText = "Insert Into MyTable ( Col1, Col2) DR> Values(1,2)"; DR> DR> comInsert.ExecuteNonQuery(); DR> DR> Col3 is an Identity column which autoincrements. How can I get the DR> value of the inserted record DR> DR> immediately after, in an multi-user environment, on a MS SQL DB? DR> DR> Thanks, DR> DR> Doru DR> --- WBR, Michael Nemtsev :: blog: http://spaces.msn.com/laflour
"At times one remains faithful to a cause only because its opponents do not cease to be insipid." (c) Friedrich Nietzsche
Hi,
It's better to use SCOPE_IDENTITY()
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Michael Nemtsev" <ne*****@msn.com> wrote in message
news:9c**************************@msnews.microsoft .com... Hello Doru,
Call SELECT @@idetity
DR> I insert a new record with a command: DR> DR> private SqlCommand comInsert; DR> DR> comInsert = conDataBase.CreateCommand(); DR> DR> comInsert.CommandType = CommandType.Text; DR> DR> comInsert.CommandText = "Insert Into MyTable ( Col1, Col2) DR> Values(1,2)"; DR> DR> comInsert.ExecuteNonQuery(); DR> DR> Col3 is an Identity column which autoincrements. How can I get the DR> value of the inserted record DR> DR> immediately after, in an multi-user environment, on a MS SQL DB? DR> DR> Thanks, DR> DR> Doru DR> --- WBR, Michael Nemtsev :: blog: http://spaces.msn.com/laflour
"At times one remains faithful to a cause only because its opponents do not cease to be insipid." (c) Friedrich Nietzsche
Thanks Ignatio
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:e6**************@TK2MSFTNGP11.phx.gbl... Hi,
It's better to use SCOPE_IDENTITY()
-- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation
"Michael Nemtsev" <ne*****@msn.com> wrote in message news:9c**************************@msnews.microsoft .com... Hello Doru,
Call SELECT @@idetity
DR> I insert a new record with a command: DR> DR> private SqlCommand comInsert; DR> DR> comInsert = conDataBase.CreateCommand(); DR> DR> comInsert.CommandType = CommandType.Text; DR> DR> comInsert.CommandText = "Insert Into MyTable ( Col1, Col2) DR> Values(1,2)"; DR> DR> comInsert.ExecuteNonQuery(); DR> DR> Col3 is an Identity column which autoincrements. How can I get the DR> value of the inserted record DR> DR> immediately after, in an multi-user environment, on a MS SQL DB? DR> DR> Thanks, DR> DR> Doru DR> --- WBR, Michael Nemtsev :: blog: http://spaces.msn.com/laflour
"At times one remains faithful to a cause only because its opponents do not cease to be insipid." (c) Friedrich Nietzsche
Here is example of a proc I just did. It uses real exceptions instead of
returning exit codes.
ALTER PROCEDURE [dbo].[InsertMessage]
@LogName nvarchar(50),
@MsgID nvarchar(50),
@RefID nvarchar(50),
@Date datetime,
@From nvarchar(50),
@Subject nvarchar(100),
@Path nvarchar(200),
@Expires datetime,
@Body nvarchar(max)
AS
begin
set nocount on
declare @logid int
set @logid = -1
begin try
-- Get LogID from LogName.
select @logid = LogID
from Logs
where LogName = @LogName
if (@logid = -1)
raiserror ('LogName does not exist.', 16, 1);
-- Insert record
insert into [Messages]
([LogID]
,[Date]
,[MsgID]
,[RefID]
,[From]
,[Subject]
,[Path]
,[Expires]
,[Body])
values
(@logid
,@Date
,@MsgID
,@RefID
,@From
,@Subject
,@Path
,@Expires
,@Body)
if (@@rowcount < 1)
raiserror ('Insert failed.', -- Msg. Error_Number will be 50000.
16, -- Severity
1); -- State
declare @seq int
set @seq = Scope_Identity()
select @seq
end try
begin catch
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
-- Use RAISERROR inside the CATCH block to return error
-- information about the original error that caused
-- execution to jump to the CATCH block.
RAISERROR (@ErrorMessage, -- Message text.
@ErrorSeverity, -- Severity.
@ErrorState -- State.
);
end catch
end
And call it from client:
public static int AddMessage(string logName, string msgID, string refID,
DateTime date, string from, string subject, string path, DateTime expires,
string body)
{
SqlCommand cmd;
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
cmd = new SqlCommand("InsertMessage", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@LogName", SqlDbType.NVarChar, 50).Value =
logName;
cmd.Parameters.Add("@MsgID", SqlDbType.NVarChar, 50).Value = msgID;
cmd.Parameters.Add("@RefID", SqlDbType.NVarChar, 50).Value = refID;
cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = date;
cmd.Parameters.Add("@From", SqlDbType.NVarChar, 50).Value = from;
cmd.Parameters.Add("@Subject", SqlDbType.NVarChar, 100).Value =
subject;
cmd.Parameters.Add("@Path", SqlDbType.NVarChar, 200).Value = path;
cmd.Parameters.Add("@Expires", SqlDbType.DateTime).Value = expires;
cmd.Parameters.Add("@Body", SqlDbType.NVarChar).Value = body;
conn.Open();
int seq = (int)cmd.ExecuteScalar();
return seq;
}
}
--
William Stacey [MVP]
On Mon, 27 Mar 2006 13:45:14 -0500, "Doru Roman" <do*******@rogers.com> wrote: Thanks Michael for the reply. Would that return only my @@identity or it might return some other user's value if that user got a new value inserted immediately after me?
"Michael Nemtsev" <ne*****@msn.com> wrote in message news:9c**************************@msnews.microsof t.com... Hello Doru,
Call SELECT @@idetity
DR> I insert a new record with a command: DR> DR> private SqlCommand comInsert; DR> DR> comInsert = conDataBase.CreateCommand(); DR> DR> comInsert.CommandType = CommandType.Text; DR> DR> comInsert.CommandText = "Insert Into MyTable ( Col1, Col2) DR> Values(1,2)"; DR> DR> comInsert.ExecuteNonQuery(); DR> DR> Col3 is an Identity column which autoincrements. How can I get the DR> value of the inserted record DR> DR> immediately after, in an multi-user environment, on a MS SQL DB? DR> DR> Thanks, DR> DR> Doru DR> --- WBR, Michael Nemtsev :: blog: http://spaces.msn.com/laflour
"At times one remains faithful to a cause only because its opponents do not cease to be insipid." (c) Friedrich Nietzsche
It certainly could return another users identity value, even from another table.
Use return scope_identity() instead:
search for scope_identity() in T-SQL Help
Otis Mukinfus http://www.arltex.com http://www.tomchilders.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Who.Really.Really.Cares |
last post by:
Hi!
I guess this must be a FAQ but I'll give it a try.
I've searched the web and usenet archive and found only negative
answers. But most of them were dated like 3-4 years back. Hasn't
anything...
|
by: Trevor Best |
last post by:
I don't know if this has been reported before but it appears to be a
bug with Access.
If I create two tables both with an identity column then create an
insert trigger on table1 that inserts a...
|
by: WhiteEagl |
last post by:
Hello,
I would need some help with this identity column problem in
SQLServer.
I have a database with two tables. The Parent table has an Identity
column.
Parent (ParentID IDENTITY, Name)...
|
by: Micheal |
last post by:
How do I get the @@identity value without using stored procedures in C#?
I have the a table named 'sports' that has an @@identity value for the
primary key.
What I want to do is after I insert...
|
by: JIM.H. |
last post by:
Hello,
Let say myTable has two fields: ID and Name. ID is an identity field and
increased sequentially by 1 each time a record is entered into table.
dr=”MayName”;...
|
by: Jason L James |
last post by:
Hi all,
I recently wrote a vb.net app using oledb to
an access database. When I inserted new
rows in my datatable the identity column
was automatically created. This app used
an un-typed...
|
by: Craig |
last post by:
In ASP.NET 2.0 and the formview control how do you get the value of the
identity field of a newly inserted record?
In ASP.NET 1.1 after the new record was saved you just said
intNewIdentityID =...
|
by: gunnar.sigurjonsson |
last post by:
Im having some problem retrieving identity value from my newly
inserted row into a view.
I have two tables T1 and T2 which I define as following
CREATE TABLE T1
(
id BIGINT GENERATED ALWAYS...
|
by: BobLewiston |
last post by:
Most databases have multiple users. For these databases it is a good idea to auto-increment the identity column.
I understand that the identity column is not incremented until the newly-created...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |