scope-identity

How do I cast Scope_Identity() to Int?

泪湿孤枕 提交于 2019-12-22 03:41:28
问题 So Scope_Identity() returns an ?-Byte Numeric Type in SQL Server. That is not awesome. Is there a safe way to cast it to an int in a select query so we don't have to manage every whim of SQL Server in our ODBC Wrapper? 回答1: If the source column to which the identity belongs is an Integer, there's no need to cast it. The following works just fine assuming the identity column is an Integer to begin with or it fits inside an "Int". DECLARE @NewIdent Int SET @NewIdent = SCOPE_IDENTITY() 回答2:

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

女生的网名这么多〃 提交于 2019-12-21 07:07:00
问题 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

Will SCOPE_IDENTITY Work in this Case?

半城伤御伤魂 提交于 2019-12-21 05:32:08
问题 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

Getting SCOPE_IDENTITY from SQL Server on Insert

馋奶兔 提交于 2019-12-11 12:32:03
问题 I guess it is too late and I'm too tired to see what I'm doing wrong. Here is what I'm trying: int imageId = imageDal.AddImage(new SqlParameter[] { new SqlParameter("@IMAGE_ID", SqlDbType.Int, Int32.MaxValue, ParameterDirection.Output, true, 0, 0,"IMAGE_ID", DataRowVersion.Current,DBNull.Value), new SqlParameter("@IMAGE", SqlDbType.Image, 11, ParameterDirection.Input, true, 0, 0,"IMAGE", DataRowVersion.Current,image) }); public int AddImage(SqlParameter[] spParams) { SqlHelper.ExecuteNonQuery

How to return the last primary key inserted

徘徊边缘 提交于 2019-12-11 12:25:55
问题 I ask about alternative or similar query in informix to perform the following: INSERT INTO days (depcode,studycode,batchnum) values (1,2,3);SELECT SCOPE_IDENTITY(); I want a query to return the SCOPE_IDENTITY() during insertion statement 回答1: I know that in t-sql you have the OUTPUT statement? Where [KEY] is the column name of your primary key and @OUT_KEY is a variable you need to declare INSERT INTO days ( depcode, studycode, batchnum ) OUTPUT INSERTED.[KEY] INTO @OUT_KEY VALUES ( 1,2,3 )

T-SQL: returning the new INSERT identity to C#

风流意气都作罢 提交于 2019-12-07 08:42:19
问题 i'm putting values into SQL Server using a Stored Procedure. The Procedure will add an ID to the row that is added. I need to get this ID back to my code. Currently I can get the I see the output id in the OUTPUT window of Visual Studio, but can't seem to capture it in my code. Here is a summarized version of the proc: SQL : CREATE PROCEDURE dbo.DoSomething ( @var1 INT = NULL, @var2 INT = NULL, @var3 DATE = NULL ) AS BEGIN INSERT INTO atable ( vara, varb, varc ) VALUES ( @var1, @var2, @var3 )

Get SCOPE_IDENTITY value when inserting bulk records for SQL TableType

穿精又带淫゛_ 提交于 2019-12-06 11:54:48
问题 I have following table structure, for convenience purpose I am only marking individual columns Table_A ( Id, Name, Desc ) Table_1 ( Id this is identity column , Name ....) Table_2 ( Id this is identity column , Table_A_Id , Table_1_Id ) The relationship between Table_1 and Table_2 is 1...* Now I have created a table type for Table_A called TType_Table_A (which only contains Id as column and from my C# app I send multiple records). I have achieved this bulk insert functionality as desired.

SQL Server OUTPUT clause

折月煮酒 提交于 2019-12-06 01:17:00
问题 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

How do I cast Scope_Identity() to Int?

余生颓废 提交于 2019-12-05 01:16:15
So Scope_Identity() returns an ?-Byte Numeric Type in SQL Server. That is not awesome. Is there a safe way to cast it to an int in a select query so we don't have to manage every whim of SQL Server in our ODBC Wrapper? If the source column to which the identity belongs is an Integer, there's no need to cast it. The following works just fine assuming the identity column is an Integer to begin with or it fits inside an "Int". DECLARE @NewIdent Int SET @NewIdent = SCOPE_IDENTITY() SELECT CAST( bigintcolumn AS int ) (Provided you know it will fit into a 32bit integer) user8020064 Just cast this

Get SCOPE_IDENTITY value when inserting bulk records for SQL TableType

一笑奈何 提交于 2019-12-04 16:57:45
I have following table structure, for convenience purpose I am only marking individual columns Table_A ( Id, Name, Desc ) Table_1 ( Id this is identity column , Name ....) Table_2 ( Id this is identity column , Table_A_Id , Table_1_Id ) The relationship between Table_1 and Table_2 is 1...* Now I have created a table type for Table_A called TType_Table_A (which only contains Id as column and from my C# app I send multiple records). I have achieved this bulk insert functionality as desired. What I need is when I insert records into Table_2 from TType_Table_A say with below statements, I would