﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » StrataFrame Application Framework - V1 » Business Objects and Data Access (How do I?)  » Creating an Upsert Stored Procedure</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Sun, 20 Sep 2026 03:35:02 GMT</lastBuildDate><ttl>20</ttl><item><title>Creating an Upsert Stored Procedure</title><link>http://forum.strataframe.net/FindPost20512.aspx</link><description>Hi SF Team,&lt;/P&gt;&lt;P&gt;In the training course and manual was a stored procedure that was referred to called an Upsert.&amp;nbsp; Could you please give me a T-SQL example of this sproc for updating/inserting a group&amp;nbsp;records at one time.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Jeff</description><pubDate>Thu, 06 Nov 2008 09:52:35 GMT</pubDate><dc:creator>Jeff Pagley</dc:creator></item><item><title>RE: Creating an Upsert Stored Procedure</title><link>http://forum.strataframe.net/FindPost20593.aspx</link><description>Hi Trent,&lt;/P&gt;&lt;P&gt;Thank you for taking the time to send me the example.&amp;nbsp; It was what I was looking for.&lt;/P&gt;&lt;P&gt;Jeff</description><pubDate>Thu, 06 Nov 2008 09:52:35 GMT</pubDate><dc:creator>Jeff Pagley</dc:creator></item><item><title>RE: Creating an Upsert Stored Procedure</title><link>http://forum.strataframe.net/FindPost20590.aspx</link><description>Jeff, it would look something like this:&lt;P&gt;[codesnippet]@userPk INT,&lt;BR&gt;@eventLocationPk BIGINT&lt;BR&gt;AS&lt;BR&gt;BEGIN&lt;/P&gt;&lt;P&gt;-- Turn off row counting&lt;BR&gt;SET NOCOUNT ON;&lt;/P&gt;&lt;P&gt;-- Declare variables&lt;BR&gt;DECLARE @recCount INT;&lt;/P&gt;&lt;P&gt;-- Perform a scalar query to determine if the &lt;BR&gt;-- record already exists&lt;BR&gt;SELECT &lt;BR&gt;&amp;nbsp;@recCount = COUNT(* )&lt;BR&gt;FROM UserLocations&lt;BR&gt;WHERE ul_us_pk = @userPk&lt;/P&gt;&lt;P&gt;-- Now determine how to update the record&lt;BR&gt;IF @recCount &amp;gt; 0 &lt;BR&gt;&amp;nbsp;BEGIN&lt;BR&gt;&amp;nbsp;&amp;nbsp;-- Update the existing record&lt;BR&gt;&amp;nbsp;&amp;nbsp;UPDATE UserLocations&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;SET ul_vel_pk = @eventLocationPk&lt;BR&gt;&amp;nbsp;&amp;nbsp;WHERE ul_us_pk = @userPk&lt;BR&gt;&amp;nbsp;END&lt;BR&gt;ELSE&lt;BR&gt;&amp;nbsp;BEGIN&lt;BR&gt;&amp;nbsp;&amp;nbsp;-- Create the location record&lt;BR&gt;&amp;nbsp;&amp;nbsp;INSERT INTO UserLocations (&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;ul_us_pk,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;ul_vel_pk)&lt;BR&gt;&amp;nbsp;&amp;nbsp; VALUES ( &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;@userPk,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;@eventLocationPk) &lt;BR&gt;&amp;nbsp;END&lt;BR&gt;&amp;nbsp;&lt;BR&gt;END&amp;nbsp;&lt;BR&gt;&amp;nbsp;[/codesnippet]</description><pubDate>Thu, 06 Nov 2008 09:24:41 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: Creating an Upsert Stored Procedure</title><link>http://forum.strataframe.net/FindPost20586.aspx</link><description>Thanks for the sproc.&amp;nbsp; However, what I was looking for is a sproc which first tries to update the record, and if the affected record count is 0, then the sproc assumes that the record does not exist to update, so&amp;nbsp;it inserts the record.&amp;nbsp; I think I know how I am going to create it, but I haven't&amp;nbsp;done it yet.&lt;/P&gt;&lt;P&gt;Jeff</description><pubDate>Thu, 06 Nov 2008 08:33:22 GMT</pubDate><dc:creator>Jeff Pagley</dc:creator></item><item><title>RE: Creating an Upsert Stored Procedure</title><link>http://forum.strataframe.net/FindPost20585.aspx</link><description>here you go&lt;/P&gt;&lt;FONT size=1&gt;&lt;P&gt;CREATE PROC [dbo].[Book_upsert]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @Book_pk int,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @Title varchar(30),&lt;/P&gt;&lt;P&gt;AS&lt;/P&gt;&lt;P&gt;SET NOCOUNT ON&lt;/P&gt;&lt;P&gt;IF @Book_pk = 0 &lt;/P&gt;&lt;P&gt;&amp;nbsp; BEGIN&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; INSERT INTO Book ([Title]) VALUES (@Title)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT SCOPE_IDENTITY() As InsertedID&lt;/P&gt;&lt;P&gt;&amp;nbsp; END&lt;/P&gt;&lt;P&gt;ELSE&lt;/P&gt;&lt;P&gt;&amp;nbsp; BEGIN&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; UPDATE Book SET [Title] = @Title WHERE [Book_pk] = @Book_pk&lt;/P&gt;&lt;P&gt;&amp;nbsp; END&lt;/P&gt;&lt;P&gt;SET NOCOUNT OFF&lt;/P&gt;&lt;/FONT&gt;</description><pubDate>Thu, 06 Nov 2008 08:08:13 GMT</pubDate><dc:creator>Bill Spack</dc:creator></item><item><title>RE: Creating an Upsert Stored Procedure</title><link>http://forum.strataframe.net/FindPost20536.aspx</link><description>Thanks Trent.&amp;nbsp; I will look in&amp;nbsp;the training manual.&lt;/P&gt;&lt;P&gt;Jeff</description><pubDate>Tue, 04 Nov 2008 06:55:15 GMT</pubDate><dc:creator>Jeff Pagley</dc:creator></item><item><title>RE: Creating an Upsert Stored Procedure</title><link>http://forum.strataframe.net/FindPost20513.aspx</link><description>UPSERTs are designed more for creating or updating a single record.&amp;nbsp; Now you may have a sproc that gets recusrivly called and updates a group of records, but generally UPSERts are designed for single record updates.&lt;/P&gt;&lt;P&gt;You might look at the training manual as I believe there is a sample in there from Ben's session.</description><pubDate>Mon, 03 Nov 2008 09:07:26 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item></channel></rss>