简体   繁体   中英

Updating Records in Table A based on Records in Table B

I have to write a statement which fills a table ( customers ) with synthetically generated values. There is an addtional constraint that I should only fill those attributes (columns) with a special property (ie formally do a projection on them and then operate on them exclusively). These properties are stored in a second table, attributes .

My first draft consists of the following two statements:

-- Get the attributes (columns) we are interested in only
SELECT attributeID from attributes
WHERE tableID = 'customers'

-- Iterate over each row of customers, filling only those attributes (columns)
-- obtained by the above SELECT statement
UPDATE customers
   SET (use the records from above select statement...)

Now my problem is how to put them together. I know there is the possibility of appending a WHERE clause to the SET clause, but that would select rows, not columns, as I need. I also read about PIVOT , but so far only inside one single table, not two, as is the case here. I would be very thankful for any hint, since I have no idea how to do this.

is not it you're looking for? SQL Update Multiple Fields FROM via a SELECT Statement

UPDATE
    Table
SET
    Table.col1 = other_table.col1,
    Table.col2 = other_table.col2
FROM
    Table
INNER JOIN
    other_table
ON
    Table.id = other_table.id

Standard SQL-92 requires a scalar subquery:

UPDATE customers
   SET attributeID = (
                      SELECT A1.attributeID 
                        FROM attributes AS A1
                       WHERE A1.tableID = 'customers'
                     );

However, UPDATE customers...WHERE A1.tableID = 'customers' "smells" like you may be mixing data with metadata.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM