Appending New Rows Only

Chad Miller, 2012-04-27

I saw a question in the forums related to inserting new rows into a SQL Server table only if they didn’t exist. The current solution was using an ADO.NET DataTable , checking for new rows and then pushing the rows back to SQL Server by calling the Update method on the DataAdapter. Although the solution works, the process becomes longer as each time the process is run the entire table is retrieved and compared.

There’s a number of approaches you could take to solve this problem. One solution is to use Table Valued Parameters which I’ve previously blogged about to push a batch of rows to SQL Server and add only new rows. This does require creating both a table type and stored procedure on the SQL Server and only works for SQL Server 2008 and higher:

<#
/* FROM SSMS */
USE tempdb
GO
/* Create a Request table for testing purposes*/
CREATE TABLE Request
( PKID INT,
 MessageText varchar(max));
GO
/* Create a RequestList table type */
CREATE TYPE RequestList AS TABLE
( PKID INT,
 MessageText varchar(max));
GO
/* Create a procedure to use insert only new rows  */
CREATE PROCEDURE uspSetRequest
    @TVP RequestList READONLY
    AS
    SET NOCOUNT ON
    INSERT Request
    SELECT tvp.PKID, tvp.MessageText
    FROM @TVP tvp
    LEFT JOIN Request r ON
    tvp.PKID = r.PKID
    WHERE r.PKID IS NULL;
 GO
 #>
#FROM Powershell
#Create an ADO.NET DataTable matching the RequestList Table Type:
$dt = new-object Data.datatable
$col =  new-object Data.DataColumn
$col.ColumnName = 'PKID'
$col.DataType = [Int32]
$dt.Columns.Add($Col)
$col =  new-object Data.DataColumn
$col.ColumnName = 'MessageText'
$col.DataType = [String]
$dt.Columns.Add($Col)
#BEGIN INSERT foreach Loops to add records to DataTable
#Example below inserts only one record
#Add a Row to the DataTable
$dr = $dt.NewRow()
$dr.Item('PKID') = 1
$dr.Item('MessageText') = 'It worked!'
$dt.Rows.Add($dr)
#END INSERT foreach Loops to add records to DataTable 
#Connection and Query Info
$serverName="$env:computernamesql1"
$databaseName='tempdb'
$query='uspSetRequest' 
#Connect
$connString = "Server=$serverName;Database=$databaseName;Integrated Security=SSPI;"
$conn = new-object System.Data.SqlClient.SqlConnection $connString
$conn.Open()
#Create Sqlcommand type and params
$cmd = new-object System.Data.SqlClient.SqlCommand
$cmd.Connection = $conn
$cmd.CommandType = [System.Data.CommandType]"StoredProcedure"
$cmd.CommandText= $query
$null = $cmd.Parameters.Add("@TVP", [System.Data.SqlDbType]::Structured)
$cmd.Parameters["@TVP"].Value = $dt
#Execute Query and close connection
$cmd.ExecuteNonQuery() | out-null
$conn.Close()

Rate

Share

Share

Rate

Related content

Database Mirroring FAQ: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup?

Question: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? This question was sent to me via email. My reply follows. Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? Databases to be mirrored are currently running on 2005 SQL instances but will be upgraded to 2008 SQL in the near future.

Robert Davis

2009-02-23

1,567 reads

Networking – Part 4

You may want to read Part 1 , Part 2 , and Part 3 before continuing. This time around I’d like to talk about social networking. We’ll start with social networking. Facebook, MySpace, and Twitter are all good examples of using technology to let…

Andy Warren

2009-02-17

1,530 reads

Speaking at Community Events – More Thoughts

Last week I posted Speaking at Community Events – Time to Raise the Bar?, a first cut at talking about to what degree we should require experience for speakers at events like SQLSaturday as well as when it might be appropriate to add additional focus/limitations on the presentations that are accepted. I’ve got a few more thoughts on the topic this week, and I look forward to your comments.

Andy Warren

2009-02-13

360 reads