我有兩個表。
表1結構NOT NULL。
表2的某些值爲NULL。
我想要更新table2到table1
我希望當選擇值,如果null不更新,並跳過下一列有更新值。
sql select更新多列並在更新前檢查不爲空
我的表
table1 value(table1) table2 value(table2)
t1ID 1234 t2ID 1234
t1Name Bear t2Name null
t1Adress 87/25 t2Adress 99/77
t1Tel 01254798535 t2Tel null
我的代碼
UPDATE table1
SET t1Name = (SELECT t2Name
FROM table2
WHERE t2Name IS NOT NULL
),
t1Adress = (SELECT t2Adress
FROM table2
WHERE t2Adress IS NOT NULL
),
t1Tel = (SELECT t2Tel
FROM table2
WHERE t2Tel IS NOT NULL
)
FROM table1,table2
WHERE t1ID = '1234' AND t2ID ='1234'
當我執行我得到錯誤:
SQL error : Msg 512, Level 16, State 1, Line 1 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated.
我怎樣才能解決呢?
@GordonLinoff謝謝更新休息。我現在可以標記。 –