Solved

SQL Unpivot update if exists else insert

Posted on 2014-12-18
6
153 Views
Last Modified: 2014-12-19
The below SQL is working to insert however, I want it to see if the items exists and if they do update them based on the xrefid and the event date for each record.

I am not sure where to begin with this.

;WITH MyCTE AS
(
    SELECT    * 
    FROM      (
                  SELECT   *
                  FROM      emdb.dbo.calendarview 
              )p
    UNPIVOT 
    ( 
        EventDate FOR DateDescription in ([Appraisal Ordered],[Closing Date],[Application Signed],[Appraisal Recieved] ,[Approval To Close],[Insurance Ordered],[Title Ordered])
    ) as unpvt
)

insert into outlookreport.dbo.calendar (eventdate,item,xrefid)
SELECT    
          M.EventDate,
          M.DateDescription,
         
          T.xrefid
FROM      emdb.dbo.calendarview T 

          JOIN MyCTE M
              ON T.xrefid = M.xrefid

Open in new window

0
Comment
Question by:desiredforsome
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 25

Expert Comment

by:chaau
ID: 40507989
If your SQL Server version is indeed 2005 you will need to use two transactions: to UPDATE and to INSERT. If you have at least version 2008 you can use MERGE. Let us know if you want to see how it would be done with the merge
0
 
LVL 25

Expert Comment

by:chaau
ID: 40508008
here is the two transaction version:
-- update first
;WITH MyCTE AS
(
    SELECT    * 
    FROM      (
                  SELECT   *
                  FROM      emdb.dbo.calendarview 
              )p
    UNPIVOT 
    ( 
        EventDate FOR DateDescription in ([Appraisal Ordered],[Closing Date],[Application Signed],[Appraisal Recieved] ,[Approval To Close],[Insurance Ordered],[Title Ordered])
    ) as unpvt
)
UPDATE c SET item = M.DateDescription
FROM emdb.dbo.calendarview T 
          JOIN MyCTE M
              ON T.xrefid = M.xrefid
INNER JOIN outlookreport.dbo.calendar c ON T.eventdate = M.EventDate AND xrefid = T.xrefid;

-- now insert
;WITH MyCTE AS
(
    SELECT    * 
    FROM      (
                  SELECT   *
                  FROM      emdb.dbo.calendarview 
              )p
    UNPIVOT 
    ( 
        EventDate FOR DateDescription in ([Appraisal Ordered],[Closing Date],[Application Signed],[Appraisal Recieved] ,[Approval To Close],[Insurance Ordered],[Title Ordered])
    ) as unpvt
)
insert into outlookreport.dbo.calendar (eventdate,item,xrefid)
SELECT    
          M.EventDate,
          M.DateDescription,
          T.xrefid
FROM      emdb.dbo.calendarview T 
          JOIN MyCTE M
              ON T.xrefid = M.xrefid
WHERE NOT EXISTS(SELECT 1 FROM outlookreport.dbo.calendar WHERE eventdate = M.EventDate AND xrefid = T.xrefid);
                                  

Open in new window

0
 

Author Comment

by:desiredforsome
ID: 40508151
Hmm... I tried to run and it is givbing me an invalid column name for eventdate and xrefid which do exist in the table that is named outlookreport.dbo.calendar so I am trying to troubleshoot but hitting walls.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:desiredforsome
ID: 40508160
WAit. i think i found the issue jsut trying to correct. Evvent date does not exists in anytable other than outlookreport.dbo.calendar.

However in the code it shows the following

INNER JOIN outlookreport.dbo.calendar c ON T.eventdate = M.EventDate AND xrefid = T.xrefid;

I think the issue is there but trying to figure out the correctiveness.
0
 
LVL 25

Accepted Solution

by:
chaau earned 500 total points
ID: 40508164
Sorry, I have messed up with the aliases. The correct query is:
-- update first
;WITH MyCTE AS
(
    SELECT    * 
    FROM      (
                  SELECT   *
                  FROM      emdb.dbo.calendarview 
              )p
    UNPIVOT 
    ( 
        EventDate FOR DateDescription in ([Appraisal Ordered],[Closing Date],[Application Signed],[Appraisal Recieved] ,[Approval To Close],[Insurance Ordered],[Title Ordered])
    ) as unpvt
)
UPDATE c SET c.item = M.DateDescription
FROM emdb.dbo.calendarview T 
          JOIN MyCTE M
              ON T.xrefid = M.xrefid
INNER JOIN outlookreport.dbo.calendar c ON c.eventdate = M.EventDate AND c.xrefid = T.xrefid;

-- now insert
;WITH MyCTE AS
(
    SELECT    * 
    FROM      (
                  SELECT   *
                  FROM      emdb.dbo.calendarview 
              )p
    UNPIVOT 
    ( 
        EventDate FOR DateDescription in ([Appraisal Ordered],[Closing Date],[Application Signed],[Appraisal Recieved] ,[Approval To Close],[Insurance Ordered],[Title Ordered])
    ) as unpvt
)
insert into outlookreport.dbo.calendar (eventdate,item,xrefid)
SELECT    
          M.EventDate,
          M.DateDescription,
          T.xrefid
FROM      emdb.dbo.calendarview T 
          JOIN MyCTE M
              ON T.xrefid = M.xrefid
WHERE NOT EXISTS(SELECT 1 FROM outlookreport.dbo.calendar WHERE eventdate = M.EventDate AND xrefid = T.xrefid);

Open in new window

0
 

Author Closing Comment

by:desiredforsome
ID: 40509131
Life Saver
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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