devxlogo

Best Method for Inserting Rows

Best Method for Inserting Rows

Question:
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?

Answer:
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

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist