How to update Table 1 from Table 2 when they have nothing in common?

独自空忆成欢 提交于 2019-12-14 03:03:01

问题


I have 2 tables - each has a single row only. I want to update o1 and o2 columns on Table1 with the corresponding columns in Table2.

create table Table1(c1 int, c2 int, o1 int, o2 int)
create table Table2(o1 int, o2 int)

I have the following, which is horrible (but works).

update Table1
set o1 = (select o1 from Table2),
    o2 = (select o2 from Table2)

Is there a better way?


回答1:


You can use:

update table1
    set o1 = t2.o1,
        o2 = t2.o2
from table2 t2;



回答2:


By this way, you can do as expected, even if there were 1 or 'n' rows-

UPDATE T1
SET T1.o1 = T2.o1,
    T1.o2 = T2.o2
from table1 T1 INNER JOIN table2 T2 ON T1.o1=T2.o1 AND T1.o2=T2.o2;


来源:https://stackoverflow.com/questions/58206904/how-to-update-table-1-from-table-2-when-they-have-nothing-in-common

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!