Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL Server
Expertise: Beginner
May 9, 2000

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 With


CREATE PROCEDURE usp_GetCategories
AS
SET NOCOUNT ON

SELECT * FROM Categories

CREATE PROCEDURE usp_AddCategory
    @CategoryName nvarchar(15),
    @Description ntext,
    @ID int OUTPUT
AS
INSERT INTO Categories (CategoryName, Description)
VALUES (@CategoryName, @Description)

Select @ID = @@identity

RETURN 0
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date