Solved

SQL Unpivot update if exists else insert

Posted on 2014-12-18
6
139 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
  • 3
  • 3
6 Comments
 
LVL 24

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 24

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 24

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

10 Experts available now in Live!

Get 1:1 Help Now