Best Method for Inserting Rows

From a database-efficiency standpoint, which is better for SQL when inserting new rows into a table: issuing ad hoc SQL statements in the form of “Insert Into…” or using the Add method on an ADO recordset object?

In general, I recommend using stored procedures to insert records, unless you have a compelling reason to update through ADO recordsets. Stored procedures are more efficient for multiple inserts, updates and deletes, and they provide more control?especially for error handling. Also, you don’t have to maintain the overhead of a server-side recordset on the database server.

Because stored procedures are compiled, SQL Server doesn’t have to interpret an insert statement each time it’s called. Frequently, I use disconnected ADO recordsets on the client to store and work with data returned from the database, but use stored procedures to update and add data. Here’s an example of using a stored procedure to retrieve records and add a new one:

Dim adoRS As New ADODB.Recordset    Dim adoCommand As New ADODB.Command        With adoRS        .ActiveConnection = "NorthwindLocal"        .Source = "usp_GetCategories"        .CursorLocation = adUseClient        .LockType = adLockBatchOptimistic        .Open                Set .ActiveConnection = Nothing                If Not .State = adStateOpen Then            MsgBox "Error returning recordset"            Exit Sub        End If                .AddNew        .Fields("CategoryName").Value = "Test Category"        .Fields("Description").Value = "Test Category"        .UpdateBatch    End With    With adoCommand        .Parameters.Append .CreateParameter("RETURN_VALUE", adInteger, _                            adParamReturnValue, , Null)        .Parameters.Append .CreateParameter("@CategoryName", adVarChar, _                            adParamInput, 15,adoRS.Fields("CategoryName").Value)        .Parameters.Append .CreateParameter("@Description", adLongVarWChar,_                            adParamInput, 2147483647,adoRS.Fields("Description").Value)        .Parameters.Append .CreateParameter("@ID", adInteger,adParamInputOutput, , Null)            .ActiveConnection = "NorthwindLocal"        .CommandText = "usp_AddCategory"        .CommandType = adCmdStoredProc        .Execute                If .Parameters("RETURN_VALUE").Value = 0 Then            MsgBox "New category " & CStr(.Parameters("@ID").Value) & _                   " added."        Else            MsgBox "Error in stored procedure execution"        End If    End WithCREATE PROCEDURE usp_GetCategoriesASSET NOCOUNT ONSELECT * FROM CategoriesCREATE PROCEDURE usp_AddCategory    @CategoryName nvarchar(15),    @Description ntext,    @ID int OUTPUTASINSERT INTO Categories (CategoryName, Description)VALUES (@CategoryName, @Description)Select @ID = @@identityRETURN 0

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS