Use of SQL OUTPUT parameter with INSERT to get Identity

        Private Const InsertToolSQL As String = "INSERT INTO [Production].[Tool] ( Bin, DateLastStockCheck, DieIntroDate, DieLastUpdate, ID, NoOfTools, Notes, QtyInAvailable, QtyInRepair, QtyOut, SetNo, Tool, ToolStoresLastUpdateNowFromToolLog, ToolSupplierID, ToolTypeID) VALUES (@Bin, @DateLastStockCheck, @DieIntroDate, @DieLastUpdate, @ID, @NoOfTools, @Notes, @QtyInAvailable, @QtyInRepair, @QtyOut, @SetNo, @Tool, @ToolStoresLastUpdateNowFromToolLog, @ToolSupplierID, @ToolTypeID); SELECT @NewID = IDENT_CURRENT(‘Production.Tool’)"
        Private Const NewIDParameterName As String = "@NewID"
            Dim command As New SqlCommand
            SetCommonParameters(cTool, command)
            command.Parameters.Add(NewIDParameterName, SqlDbType.Int)
            command.Parameters(NewIDParameterName).Direction = ParameterDirection.Output
                ExecuteNonQuery(command, InsertToolSQL)
                cTool.ID = CType(command.Parameters(NewIDParameterName).Value, Integer)
            Catch ex As Exception
                Throw ex
            End Try

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

<span>%d</span> bloggers like this: