FredTheDog
FredTheDog

Reputation: 63

SQL Server - UPDATE data based on SELECT

I have written the following which returns a list of Buildings that have only one room, but the area of that room (fma0.area) is not equal to the area of the building (fmb0.nia)

select 
rtrim(fma0.bldgcode) As bldgcode
from fma0 
left join fmb0 on fma0.bldgcode = fmb0.bldgcode
where fma0.bldgcode in (
    select fma0.bldgcode
    from fma0 
    left join fmb0 on fma0.bldgcode = fmb0.bldgcode
    where fmb0.bldgstatus = ''
    group by fma0.bldgcode
    having count(fma0.auto_key) = 1
)
and round(fma0.area,0) <> fmb0.nia
and fmb0.nia > 0
order by 1

I need to use this list of buildings to UPDATE several fields in the FMA0 table (FMA0.GROSS, FMA0.AREA, FMA0.RENTABLE) for each BLDGCODE with the value from FMB0.NIA for the same BLDGCODE

How do I convert this into an UPDATE statement that looks up the FMB0.NIA value for each BLDGCODE and updates the value in each field for the same BLDGCODE in the FMA0 table

Thanks

Upvotes: 0

Views: 350

Answers (2)

Akshay I
Akshay I

Reputation: 4175

Under most circumstances, SQL updates are performed using direct references to a particular table (UPDATE books SET books.title = 'The Hobbit' WHERE books.id = 1). Yet, on occasion, it may prove beneficial to alter the contents of a table indirectly, by using a subset of data obtained from secondary query statement.

Performing an UPDATE using a secondary SELECT statement can be accomplished in one of two ways, primarily depending upon which version of SQL Server you are using. We’ll briefly explore both options so you can find what works best for you.

Using INNER JOINS

For all SQL Server installations, the most basic method of performing this action is to use an INNER JOIN, whereby values in the columns of two different tables are compared to one another.

UPDATE
  books
SET
  books.primary_author = authors.name
FROM
  books
INNER JOIN
  authors
ON
  books.author_id = authors.id
WHERE
  books.title = 'The Hobbit'

In the above example, we’re UPDATING the books.primary_author field to match the authors.name for ‘The Hobbit’ by JOINING both tables in the query to their respective, matching values of authors.id and books.author_id.

Using MERGE to UPDATE and INSERT Simultaneously

For SQL Server 2008 and newer, Microsoft introduced the exceptionally useful MERGE operation which is similar to the above INNER JOIN method, but MERGE attempts to perform both an UPDATE and an INSERT command together. This effectively synchronizes the two tables based on the query performed, updating and inserting records as necessary for the two to match.

MERGE INTO
  books
USING
  authors
ON
  books.author_id = authors.id
WHEN MATCHED THEN
  UPDATE SET
    books.primary_author = authors.name
WHEN NOT MATCHED THEN
  INSERT
    (books.author_id, books.primary_author)
  VALUES
    (authors.id, authors.name)

The full query when using MERGE is certainly a bit more complex then that of a basic INNER JOIN, but once you grasp how the operation functions, you’ll quickly understand how powerful this capability can truly be.

The first few lines are rather self-explanatory:

MERGE INTO
  books
USING
  authors
ON
  books.author_id = authors.id

We want to MERGE INTO (UPDATE/INSERT) the books table by using the secondary authors table, and we’re matching the two based on the same books.author_id = authors.id comparison.

Where the MERGE command differs is in the branching logic that follows.

WHEN MATCHED THEN
  UPDATE SET
    books.primary_author = authors.name

Here we’re asking SQL to perform an action only when records MATCHED – when an existing record is found. In that case, we perform a standard UPDATE just as we did before, setting the books.primary_author field to equal the authors.name field.

Finally, if the query discovers a matching comparative record that doesn’t exist, we instead perform an INSERT.

WHEN NOT MATCHED THEN
  INSERT
    (books.author_id, books.primary_author)
  VALUES
    (authors.id, authors.name)

Here we’re simply asking SQL to INSERT a new record into the books table and passing along the values for the author_id and primary_author fields, grabbed from the associated authors table record.

The end result of our MERGE statement is that for every author in the authors table, we verify whether a corresponding book exists in books. If a record is found, we ensure books.primary_author is set using UPDATE, and where no match is found, we add a new record to books.

With that, you should have a solid understanding of two different methods that can be used to UPDATE records in SQL by using secondary, comparative SELECT statements.

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269603

This seems like a much simpler way to get the buildings that you want:

select b.bldcode
from fmbo b join
     (select r.bldgcode, max(r.area) as room_area
      from fma0 r
      group by r.bldgcode
      having count(*) = 1
     ) r 
     on r.bldgcode = b.bldgcode and r.room_area <> b.nia;

The first subquery gets the area of rooms in buildings that have only one room. The join then simply combines them according to your rules.

This is readily turned into an update:

update b
    set . . .
    from fmbo b join
         (select r.bldgcode, max(r.area) as room_area
          from fma0 r
          group by r.bldgcode
          having count(*) = 1
         ) r 
         on r.bldgcode = b.bldgcode and r.room_area <> b.nia;

Upvotes: 2

Related Questions