?
Solved

SQL Unpivot update if exists else insert

Posted on 2014-12-18
6
Medium Priority
?
166 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 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

There are some very powerful Dynamic 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 di…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
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 anti-spam), the admin…
Loops Section Overview

864 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