将一个表的完整列数据插入到Microsoft SQL的另一个表中的特定列。

时间:2021-08-21 15:41:19

I m having two tables COMPONENTS(sno,comp_name,quantity,costperunit,totalcost) and COST(sno,comp_name,costperunit).. in Microsfot SQL Server

我有两个表组件(sno、comp_name、quantity、costperunit、totalcost)和COST(sno、comp_name、costperunit)。Microsfot SQL服务器中

I m prompting user to enter costperunit value and updating immediately in COST table. I want to import all the values of costperunit column to COMPONENTS from COST Table.

我正在提示用户输入costperunit值并在成本表中立即更新。我想从成本表中导入costperunit列的所有值。

I have tried this :

我试过:

insert into COMPONENTS(costperunit) 
    select costperunit from COST where COST.sno=COMPONENTS.sno

but I m unable to achieve the required functionality.

但我无法实现所需的功能。

Somebody please suggest me better query to do this.. Thanks in Advance..

3 .谁能告诉我该怎么办?提前谢谢. .

1 个解决方案

#1


0  

If you want to update (!) all values, why are you trying to insert ? Try to use "update from select" way:

如果要更新(!)所有值,为什么要插入?尝试使用“选择更新”方式:

UPDATE components
SET components.costperunit=cost.costperunit
FROM cost
INNER JOIN components ON cost.sno=components.sno

Even better to add WHERE clause to update only changed values:

最好添加WHERE子句以只更新已更改的值:

#1


0  

If you want to update (!) all values, why are you trying to insert ? Try to use "update from select" way:

如果要更新(!)所有值,为什么要插入?尝试使用“选择更新”方式:

UPDATE components
SET components.costperunit=cost.costperunit
FROM cost
INNER JOIN components ON cost.sno=components.sno

Even better to add WHERE clause to update only changed values:

最好添加WHERE子句以只更新已更改的值: