如何将数据添加到具有来自另一个表的空值的列中?

时间:2019-04-23 07:59:36

标签: sql-server

我正在尝试用收集的数据填充表。

我有三个表,INCIDENT_MAIN,INCIDENT_REPORT和PROJECT_PROFILE。

我必须从INCIDENT_MAIN和INCIDENT_REPORT提供的数据中填写PROJECT_PROFILE表。

这是我进行INNER JOIN的查询

INSERT INTO PROJECT_PROFILE (INCIDENT_REPORT_ID, PROJECT_NAME,PROJECT_TYPE, PROJECT_PHASE, PROJECT_START_DATE,PROJECT_END_DATE,DOMESTIC_INTERNATIONAL,PROJECT_FACILITIES_LOGISTIC)
  SELECT PROJECT_NAME, PROJECT_TYPE, PROJECT_PHASE, PROJECT_START_DATE, ESTIMATED_PROJECT_END_DATE, DOMESTIC_INTERNATIONAL, PROJECT_FACILITIES_LOGISTIC
  FROM INCIDENT_MAIN INCIDENT_REPORT_ID
  INNER JOIN INCIDENT_REPORT ON INCIDENT_REPORT.INCIDENT_REPORT_ID = PROJECT_PROFILE.INCIDENT_REPORT_ID

我收到了多部分标识符'PROJECT_PROFILE.INCIDENT_REPORT_ID'无法绑定的错误消息

因此,我决定首先用INCIDENT_REPORT_ID填充PROJECT_PROFILE表,以便可以从INCIDENT_REPORT表中收集INCIDENT_REPORT_ID的数据。因为我将PROJECT_PROFILE_ID设置为IDENTITY KEY,所以它是自动递增的。

数据已正确插入。结果如下。

enter image description here

受影响的234行。

现在,我尝试用INCIDENT_MAIN中的数据替换NULL值

这是查询

INSERT INTO PROJECT_PROFILE (PROJECT_NAME,PROJECT_TYPE, PROJECT_PHASE, PROJECT_START_DATE,PROJECT_END_DATE,DOMESTIC_INTERNATIONAL,PROJECT_FACILITIES_LOGISTIC)
  SELECT PROJECT_NAME, PROJECT_TYPE, PROJECT_PHASE, PROJECT_START_DATE, ESTIMATED_PROJECT_END_DATE, DOMESTIC_INTERNATIONAL, PROJECT_FACILITIES_LOGISTIC
  FROM INCIDENT_MAIN
  WHERE PROJECT_PROFILE_ID >='1'

但是现在,将数据形式INCIDENT_MAIN插入到234的最后一行下,这会使PROJECT_PROFILE_ID增加,并且未替换空值。

enter image description here

执行查询的正确方法是什么,以便可以将NULL值替换为实际数据,而PROJECT_PROFILE_ID不会增加。

已编辑

因此,我根据建议尝试了此更新查询

UPDATE PROJECT_PROFILE
SET PROJECT_PROFILE.PROJECT_NAME = INCIDENT_MAIN.PROJECT_NAME,
PROJECT_PROFILE.PROJECT_PHASE = INCIDENT_MAIN.PROJECT_PHASE,
PROJECT_PROFILE.PROJECT_START_DATE = INCIDENT_MAIN.PROJECT_START_DATE,
PROJECT_PROFILE.PROJECT_END_DATE = INCIDENT_MAIN.ESTIMATED_PROJECT_END_DATE,
PROJECT_PROFILE.DOMESTIC_INTERNATIONAL = INCIDENT_MAIN.DOMESTIC_INTERNATIONAL,
PROJECT_PROFILE.PROJECT_FACILITY_LOGISTIC = INCIDENT_MAIN.PROJECT_FACILITIES_LOGISTIC
FROM PROJECT_PROFILE JOIN INCIDENT_MAIN ON
PROJECT_PROFILE.PROJECT_NAME= INCIDENT_MAIN.PROJECT_NAME
WHERE PROJECT_PROFILE.PROJECT_PROFILE_ID <='234'

获取(受影响的0行)

1 个答案:

答案 0 :(得分:1)

在不知道您的表模式的情况下,我只是在猜测...

我已将JOIN条件更改为INCIDENT_REPORT.INCIDENT_REPORT_ID = INCIDENT_MAIN.INCIDENT_REPORT_ID。如果列名错误,请相应修改

INSERT INTO PROJECT_PROFILE 
            (
                -- INCIDENT_REPORT_ID,  remove this line
                PROJECT_NAME,
                PROJECT_TYPE, 
                PROJECT_PHASE, 
                PROJECT_START_DATE,
                PROJECT_END_DATE,
                DOMESTIC_INTERNATIONAL,
                PROJECT_FACILITIES_LOGISTIC
            )
SELECT     PROJECT_NAME, 
           PROJECT_TYPE, 
           PROJECT_PHASE,  
           PROJECT_START_DATE, 
           ESTIMATED_PROJECT_END_DATE, 
           DOMESTIC_INTERNATIONAL, 
           PROJECT_FACILITIES_LOGISTIC
FROM       INCIDENT_MAIN INCIDENT_REPORT_ID
INNER JOIN INCIDENT_REPORT 
        ON INCIDENT_REPORT.INCIDENT_REPORT_ID = INCIDENT_MAIN.INCIDENT_REPORT_ID 

我的建议是,不要插入表格中。首先,您执行查询,从SEELCT开始一直到结尾。确保结果是您想要的。然后添加INSERT语句。