web-dev-qa-db-ja.com

SQL Serverの別のテーブルの値を使用してテーブルを更新する

データベースに2つのテーブルがあります。

表1

-------------------------------------------------------------------------
| name | family | phone | email | gender | phone2 | address | birthdate |
-------------------------------------------------------------------------

テーブル#2

-----------------------------------------
| gender | address | phone | birthdate |
-----------------------------------------

表#1の列addressおよびphone2は空で、列genderおよびbirthdateの値は表#2と同じです。

テーブル#2からデータを読み取り、テーブル#1のaddressおよびphone2をテーブル#2の値で更新するにはどうすればよいですかaddressおよびphonegenderおよびbirthdateの場合の列は各行で同じですか?

例:これは表#1の一部のデータです

-------------------------------------------------------------------------
| name | family | phone | email | gender | phone2 | address | birthdate |
-------------------------------------------------------------------------
| john | doe    | 12345| [email protected]| Male  |         |         | 1980-01-01|
-------------------------------------------------------------------------
| mike | clark  | 65432| [email protected]| Male  |         |         | 1990-01-01|
-------------------------------------------------------------------------
| Sara | King   | 875465| [email protected]|Female|         |         | 1970-01-01|
-------------------------------------------------------------------------

そして、ここにテーブル#2のいくつかのデータがあります

-----------------------------------------
| gender | address | phone | birthdate  |
-----------------------------------------
| Male   | 1704test|0457852|1980-01-01  |
-----------------------------------------
| Female | 1705abcs|0986532|1970-01-01  |
-----------------------------------------
| Male   | 1602cyzd|0326589|1990-01-01  |
-----------------------------------------

テーブル#1をテーブル#2のデータで更新し、genderおよびbirthdateを確認して、テーブル#1を次のようにします

-------------------------------------------------------------------------
| name | family | phone | email | gender | phone2 | address | birthdate |
-------------------------------------------------------------------------
| john | doe    | 12345| [email protected]| Male   |0457852 |1704test | 1980-01-01|
-------------------------------------------------------------------------
| mike | clark  | 65432| [email protected]| Male   |0326589  |1602cyzd| 1990-01-01|
-------------------------------------------------------------------------
| Sara | King   | 875465| [email protected]|Female |0986532  |1705abcs| 1970-01-01|
-------------------------------------------------------------------------

これどうやってするの?

11
John Doe

希望する結果を得るには、いくつかの方法があります。

不確定な方法

(テーブル2の多くの行がテーブル1の1つと一致する場合)

UPDATE T1
SET    address = T2.address,
       phone2 = T2.phone
FROM   #Table1 T1
       JOIN #Table2 T2
         ON T1.gender = T2.gender
            AND T1.birthdate = T2.birthdate

または、もう少し簡潔な形式

UPDATE #Table1
SET    address = #Table2.address,
       phone2 = #Table2.phone
FROM   #Table2
WHERE  #Table2.gender = #Table1.gender
       AND #Table2.birthdate = #Table1.birthdate 

またはCTEで

WITH CTE
     AS (SELECT T1.address AS tgt_address,
                T1.phone2  AS tgt_phone,
                T2.address AS source_address,
                T2.phone   AS source_phone
         FROM   #Table1 T1
                INNER JOIN #Table2 T2
                  ON T1.gender = T2.gender
                     AND T1.birthdate = T2.birthdate)
UPDATE CTE
SET    tgt_address = source_address,
       tgt_phone = source_phone 

決定論的方法

MERGEは、非決定的な結果を受け入れるのではなく、エラーをスローします

MERGE #Table1 T1
USING #Table2 T2
ON T1.gender = T2.gender
   AND T1.birthdate = T2.birthdate
WHEN MATCHED THEN
  UPDATE SET address = T2.address,
             phone2 = T2.phone; 

または、複数の一致がある場合、特定のレコードを選択できます

APPLY

UPDATE T1
SET    address = T2.address,
       phone2 = T2.phone
FROM   #Table1 T1
       CROSS APPLY (SELECT TOP 1 *
                    FROM   #Table2 T2
                    WHERE  T1.gender = T2.gender
                           AND T1.birthdate = T2.birthdate
                    ORDER  BY T2.PrimaryKey) T2 

..またはCTE

WITH T2
     AS (SELECT *,
                ROW_NUMBER() OVER (PARTITION BY gender, birthdate ORDER BY primarykey) AS RN
         FROM   #Table2)
UPDATE T1
SET    address = T2.address,
       phone2 = T2.phone
FROM   #Table1 T1
       JOIN T2
         ON T1.gender = T2.gender
            AND T1.birthdate = T2.birthdate
            AND T2.RN = 1;
24
Martin Smith
UPDATE TS
SET TS.TaskFullAddress = L.FullAddress
FROM [dbo].[TaskOrders]   TS
INNER JOIN Locations L
ON  TS.ClientId  = L.ClientId;
0
David Fawzy