0

I have a complex(*) SQL SELECT statement that returns the rows I require to be updated

How do I update the rows returned by my SELECT statement only?

All the posts I see about updating rows using a SELECT statement talk about INNER JOIN against another table using fields to match (How do I UPDATE from a SELECT in SQL Server?)

My statement is

SELECT * FROM table1 I 
INNER JOIN
(SELECT *
  FROM (
                SELECT  INTNO,EFFDTE,
                        ROW_NUMBER() OVER(PARTITION BY INTNO,EFFDTE ORDER BY EFFDTE DESC) rn
                    FROM table2 WHERE REFID = 8888 AND EFFDTE IS NOT NULL AND INTNO <> 1234567
              ) a
WHERE rn = 1) X
ON X.INTNO = I.MEMBNO AND I.ENDDTE = DATEADD(DD,-1,X.EFFDTE)
WHERE I.ENDRSN = 'abc'
Community
  • 1
  • 1
Mike
  • 2,391
  • 6
  • 33
  • 72

3 Answers3

1

Use this syntax

UPDATE a
SET    a.col1 = b.col1,
       a.col1 = b.col2
       .....
       .....
FROM   update_table a
       JOIN(SELECT *
            FROM   table1 I
                   INNER JOIN (SELECT INTNO,
                                      EFFDTE,
                                      Row_number()
                                        OVER(
                                          PARTITION BY INTNO, EFFDTE
                                          ORDER BY EFFDTE DESC) rn
                               FROM   table2
                               WHERE  REFID = 8888
                                      AND EFFDTE IS NOT NULL
                                      AND INTNO <> 1234567) X
                           ON X.INTNO = I.MEMBNO
                              AND I.ENDDTE = Dateadd(DD, -1, X.EFFDTE)
            WHERE  I.ENDRSN = 'abc'
                   AND rn = 1) b
         ON a.common_colum = b.common_colum 
Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
  • Thanks. What happens if I do not have a common column? ie: a value that only appears in my returned rows from my SELECT statement? My SELECT statement might return 3 of the 10 rows that have INTNO 111222 but I only want to update the 3 of them, not all 10 – Mike Jan 12 '15 at 09:09
  • Thanks @NoDisplayName - I realised that my SELECT statement WAS the answer. I was trying to JOIN my entire select statement to an update whereas I needed to step back slightly and use the update against it. (I've added the answer) – Mike Jan 12 '15 at 09:33
0

Try this

Update table1 
set Col_Name=Value
FROM table1 I 
INNER JOIN
(SELECT *
  FROM (
                SELECT  INTNO,EFFDTE,
                        ROW_NUMBER() OVER(PARTITION BY INTNO,EFFDTE ORDER BY EFFDTE DESC) rn
                    FROM table2 WHERE REFID = 8888 AND EFFDTE IS NOT NULL AND INTNO <> 1234567
              ) a
WHERE rn = 1) X
ON X.INTNO = I.MEMBNO AND I.ENDDTE = DATEADD(DD,-1,X.EFFDTE)
WHERE I.ENDRSN = 'abc'
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
0

It was a matter of approach and understanding how to best approach SQL queries.

I do not need to join to the entire SELECT I had built, I just need to replace the SELECT with the UPDATE target so that the join against the update uses the logic (and common columns)

UPDATE table1
SET field1 = 'MP', field2 = NULL
FROM 
table1 I JOIN
(SELECT *
  FROM (
                SELECT  INTNO,EFFDTE,
                        ROW_NUMBER() OVER(PARTITION BY INTNO,EFFDTE ORDER BY EFFDTE DESC) rn
                    FROM table2 WHERE REFID = 8888 AND EFFDTE IS NOT NULL AND INTNO <> 1234567
              ) a
WHERE rn = 1) X
ON X.INTNO = I.MEMBNO AND I.ENDDTE = DATEADD(DD,-1,X.EFFDTE)
WHERE I.ENDRSN = 'abc'
Mike
  • 2,391
  • 6
  • 33
  • 72