从View更新表

时间:2016-01-25 19:21:00

标签: ms-access view sql-update

我有以下名为cont_struct_breaks的视图:

pipe_segment_reference  com_struct_score    com_defects com_struct_index
2610                         353               111            3.2
3988                         266               97             2.7
5632                         49                22             2.2

我有另一个名为structural_rating的表,其中包含字段

pipe_segment_reference, structural_score structural_defects, structural_index   

我想更新“cont_truct_breaks”中的“structural_rating”中的值,其中pipe_segment_reference匹配。

然而我无法弄清楚这一点,我厌倦了做这样的事情,但没有运气。

Attempted This

[编辑]

使用select语句创建cont_struct_breaks视图,如下所示:

SELECT
    structural_rating.Pipe_Segment_Reference,
    cont_struct + structural_score AS com_struct_score,
    cont_struct_d + structural_defects AS com_defects,
    ROUND(com_struct_score / com_defects, 1) AS com_struct_index
FROM ...

如果比视图更容易,请从上面的select语句中更新structural_ratings表。

2 个答案:

答案 0 :(得分:0)

直接替换你的信息,以获得该链接答案中答案的内容,我得到了这个:

UPDATE
  structural_rating
SET
  structural_rating.structural_score = RAN.com_struct_score
FROM
  structural_rating SI
INNER JOIN
  cont_struct_breaks RAN
ON 
  SI.pipe_segment_reference = RAN.pipe_segment_reference

您可能希望更改别名的名称以减少混淆,但它的工作方式会很好。

所以现在要更新其他字段。当您一次更新多个字段时,只需用逗号分隔它们:

SET
  structural_rating.structural_score = RAN.com_struct_score,
  structural_rating.structural_defects = RAN.com_defects,
  structural_rating.structural_index = RAN.com_struct_index

因此,您的完整查询应为:

UPDATE
  structural_rating
SET
  structural_rating.structural_score = RAN.com_struct_score,
  structural_rating.structural_defects = RAN.com_defects,
  structural_rating.structural_index = RAN.com_struct_index
FROM
  structural_rating SI
INNER JOIN
  cont_struct_breaks RAN
ON 
  SI.pipe_segment_reference = RAN.pipe_segment_reference

答案 1 :(得分:0)

我最后写了这个写新表而不是查看然后运行它:

UPDATE structural_rating
INNER JOIN cont_struct
ON structural_rating.pipe_segment_reference = cont_struct.pipe_segment_reference
SET
structural_score = cont_struct.com_struct_score,    
structural_defects = cont_struct.com_defects,     
structural_index = cont_struct.com_struct_index;