Solved

If row exists in table update else insert a new row

Posted on 2014-12-17
5
27 Views
Last Modified: 2016-06-16
I have the following stored procedure:

USE [DATABASE]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[InsertNewRow]
      @StartDate DateTime = null

AS
  if @StartDate is null
        set @StartDate = getdate()

Begin

Insert [dbo].[TempPortfolioHistory]

EXEC CITYCRM_MSCRM.dbo.PortfolioHistory @EndDate = @StartDate

End

It adds a new row to the table.

Now the SP is going to be run more than once a day so I need to update the row if there is a row with the same date (first column in the row of the table), otherwise I need to do what the SP does currently.

Any help would be appreciated.
0
Comment
Question by:plainwrapname
  • 2
5 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 334 total points
Comment Utility
Wasn't sure what type of UPDATE you'd need/want to do, but basically you'll want to use [NOT] EXISTS to test for the row.

ALTER PROCEDURE [dbo].[InsertNewRow]
       @StartDate DateTime = null

AS
SET NOCOUNT ON
IF @StartDate is null
    SET @StartDate = GETDATE()

IF NOT EXISTS(SELECT 1 FROM [dbo].[TempPortfolioHistory] WHERE StartDate = @StartDate)
Begin
    Insert [dbo].[TempPortfolioHistory]
    EXEC CITYCRM_MSCRM.dbo.PortfolioHistory @EndDate = @StartDate
End

GO --end of proc
0
 

Author Comment

by:plainwrapname
Comment Utility
Sorry I was not clear.  If the row exists with the same date, I wish to overwrite that row instead of doing the insert.  I guess I need clarification on the use of the UPDATE command.
0
 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 334 total points
Comment Utility
The only info directly available from the proc is the @StartDate, and that's used to do the search.  There's no data to "UPDATE".  You might need to change the stored proc to handle updates, or create a separate proc for updates and then you call the proc you need, either INSERT or UPDATE.
0
 
LVL 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 166 total points
Comment Utility
What returns the SP CITYCRM_MSCRM.dbo.PortfolioHistory ?
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now