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

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes