Solved

SQL Unpivot update if exists else insert

Posted on 2014-12-18
6
145 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Stored procedure query with if 27 36
SQL HELP 2 82
Change variables in SQL table 6 81
Pivot Query Problem 9 37
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
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, f…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

22 Experts available now in Live!

Get 1:1 Help Now