scope-identity

SQL Server OUTPUT clause

淺唱寂寞╮ 提交于 2019-12-04 06:55:31
I am a little stuck with why I can not seem to get the 'new identity' of the inserted row with the statement below. SCOPE_IDENTITY() just returns null. declare @WorkRequestQueueID int declare @LastException nvarchar(MAX) set @WorkRequestQueueID = 1 set @LastException = 'test' set nocount off DELETE dbo.WorkRequestQueue OUTPUT DELETED.MessageEnvelope, DELETED.Attempts, @LastException, GetUtcdate(), -- WorkItemPoisened datetime DELETED.WorkItemReceived_UTC INTO dbo.FaildMessages FROM dbo.WorkRequestQueue WHERE WorkRequestQueue.ID = @WorkRequestQueueID IF @@ROWCOUNT = 0 RAISERROR ('Record not

Is there any way to use SCOPE_IDENTITY if using a multiple insert statement?

元气小坏坏 提交于 2019-12-03 22:20:30
I will import many data rows from a csv file into a SQL Server database (through a web application). I need the auto generated id value back for the client. If I do this in a loop, the performance is very bad (but I can use SCOPE_IDENTITY() without any problems). A more performant solution would be a way like this: INSERT INTO [MyTable] VALUES ('1'), ('2'), ('3') SELECT SCOPE_IDENTITY() Is there any way to get all generated IDs and not only the last generated id? Thanks for your help! Best regards, Thorsten No, SCOPE_IDENTITY() only gives you the one, latest inserted IDENTITY value. But you

Will SCOPE_IDENTITY Work in this Case?

大憨熊 提交于 2019-12-03 17:03:38
I have PK that is self incrementing key. I need to insert the record into the database and then get that PK back and use it in another insert. However I would like to do this in one transaction. Is that possible. The idea is that if something fails in any of the updates/inserts I have to do then I can rollback everything but I am under the impression that I need to do a commit. I was going to do it in ado.net at first but then switched to a stored procedure since I thought maybe that would get around this issue. Will a SP help me out in this case? Yes, scope_identity will give you the latest

Scope_identity() in batched sqlclient commands

孤街浪徒 提交于 2019-12-02 06:25:22
I usually use a stored procedure when inserting records to make sure I get the correct scope_identity() value. I have a requirement to get the id field of an inserted record when using SqlClient now. My understanding is that if I batch the scope_identity() command with the insert then it will still be in the same scope as the insert command? Something like below. Hard to verify though... Will I 100% get the correct id value with this..? (id field is an auto-incrementing bigint - Sql Server) long newid = 0; using (SqlConnection conn = new SqlConnection(....)) { conn.Open(); using (SqlCommand

Entity Framework and SCOPE_IDENTITY

守給你的承諾、 提交于 2019-12-02 06:09:44
问题 I have a stored procedure that inserts into a table then executes this line SET @returnVal = SCOPE_IDENTITY(); and after that I've tried both: SELECT @returnVal and return @returnVal When I execute the stored procedure from Microsoft SQL Server Management Studio, I get the expected result with SELECT @returnVal - the identity column for the inserted data is selected. However when I add the stored procedure to my ADO.Net Entity Data Model / EntityFramework class / .edmx class and execute the

Entity Framework and SCOPE_IDENTITY

非 Y 不嫁゛ 提交于 2019-12-02 01:56:46
I have a stored procedure that inserts into a table then executes this line SET @returnVal = SCOPE_IDENTITY(); and after that I've tried both: SELECT @returnVal and return @returnVal When I execute the stored procedure from Microsoft SQL Server Management Studio, I get the expected result with SELECT @returnVal - the identity column for the inserted data is selected. However when I add the stored procedure to my ADO.Net Entity Data Model / EntityFramework class / .edmx class and execute the stored procedure in my C# code, I get the value -1 returned without fail. Is it possible to get the

What data type does the SQLCommand method ExecuteScalar() return?

时光怂恿深爱的人放手 提交于 2019-12-01 18:18:18
In SQL Server, ID is a not null integer, and an identity. When I run the following code, I get an InvalidCastException on the last line: SqlCommand cmd = new SqlCommand(); cmd.Connection = _conn; cmd.CommandText = @"INSERT INTO [Users] (Name, Email, Password) VALUES (@name, @email, @pass); SELECT SCOPE_IDENTITY()"; cmd.Parameters.AddWithValue("@name", newUser.Name); cmd.Parameters.AddWithValue("@email", newUser.Email); cmd.Parameters.AddWithValue("@pass", newUser.PasswordHash); int id = (int)cmd.ExecuteScalar(); What is ExecuteScalar() returning here? Whatever its returning has a ToString()

What data type does the SQLCommand method ExecuteScalar() return?

只谈情不闲聊 提交于 2019-12-01 17:49:26
问题 In SQL Server, ID is a not null integer, and an identity. When I run the following code, I get an InvalidCastException on the last line: SqlCommand cmd = new SqlCommand(); cmd.Connection = _conn; cmd.CommandText = @"INSERT INTO [Users] (Name, Email, Password) VALUES (@name, @email, @pass); SELECT SCOPE_IDENTITY()"; cmd.Parameters.AddWithValue("@name", newUser.Name); cmd.Parameters.AddWithValue("@email", newUser.Email); cmd.Parameters.AddWithValue("@pass", newUser.PasswordHash); int id = (int

Fetch scope_identity value in C# code from stored procedure in 3 tier architecture

拟墨画扇 提交于 2019-11-28 02:17:45
I need to fetch scope_identity() value from a stored procedure after inserting data in C# code. Here I am using 3 tier architecture. Please anyone help me protected void Submit_Click(object sender, EventArgs e) { this.CreateUserGroups(strGroupName, strDescription, strGroupType); } protected void CreateUserGroups(string strGroupName, string strDescription, string strGroupType) { string strReturn = string.Empty; strReturn = objUser.SaveCreateGroups(strGroupName, strDescription, strGroupType); } public string SaveCreateGroups(string strGroupName, string strDescription, string strGroupType) { try

Why does select SCOPE_IDENTITY() return a decimal instead of an integer?

五迷三道 提交于 2019-11-26 18:52:33
So I have a table with an identity column as the primary key, so it is an integer. So, why does SCOPE_IDENTITY() always return a decimal value instead of an int to my C# application? This is really annoying since decimal values will not implicitly convert to integers in C#, which means I now have to rewrite a bunch of stuff and have a lot of helper methods because I use SQL Server and Postgres, which Postgres does return an integer for the equivalent function.. Why does SCOPE_IDENTITY() not just return a plain integer? Are there people out there that commonly use decimal/non-identity values