我的合并语句无法正常工作

时间:2018-12-12 00:06:02

标签: sql plsql

我想将两个给定表合并为一个临时表。我运行了几次合并语句,但运行不正常。
我的Stage_Project具有在lds_project中可以找到但在Mch_job中找不到的FeedbackValue。
我应该怎么解决这个问题?

下一个问题是我想在无法应用的合并语句中使用某些if条件


合并声明

MERGE INTO  Stage_Project s
    USING (Select project_id,pj_short_desc,pj_actual_start_date,pj_actual_end_date,pj_estimated_end_date,feedback_value from lds_project Union 
    Select  project_id,prj_short_desc pj_short_desc,prj_actual_start_date pj_actual_start_date ,prj_actual_end_date pj_actual_end_a,prj_estimated_end_date pj_estimate_end_date from Mch_job) u
    ON (s.Stage_Project_Key = u.project_id)
    WHEN MATCHED THEN
      UPDATE SET
    Stage_Project_Desc   = pj_short_desc,
    Stage_Estimated_End_Date  =pj_actual_start_date,
    Stage_Actual_End_Date   =pj_actual_end_date,
    Stage_FeedbackValue    =feedback_value,
    Stage_Actual_Start_Date =actual_start_date,
    Stage_LdMch_Id    =project_id,
    Stage_Source    =if(leeds)="lds" || if(mch)="mch",
    WHEN NOT MATCHED THEN
      INSERT (Stage_Project_Desc,
    Stage_Estimated_End_Date,
    Stage_Actual_End_Date,
    Stage_FeedbackValue,
    Stage_Actual_Start_Date,
    Stage_LdMch_Id,
    Stage_Source)
      VALUES (pj_short_desc,pj_estimated_end_date ,pj_actual_end_date ,feedback_value,pj_actual_start_date,project_id,if(leeds)="lds" || if(mch)="mch");​

登台表

CREATE TABLE Stage_Project(
    Stage_Project_Key    INTEGER NOT NULL,
    Stage_Project_Desc    VARCHAR(50),
    Stage_Estimated_End_Date    DATE,
    Stage_Actual_End_Date    DATE,
    Stage_FeedbackValue    INTEGER,
    Stage_Actual_Start_Date    DATE,
    Stage_LdMch_Id    INTEGER,
    Stage_Source    VARCHAR(50),
    -- Specify the PRIMARY KEY constraint for table "Stage_Project".
    -- This indicates which attribute(s) uniquely identify each row of data.
    CONSTRAINT    pk_Stage_Project PRIMARY KEY (Stage_Project_Key)
);

Mch_job

-- Create a Database table to represent the "Mch_Job" entity.
CREATE TABLE Mch_Job(
    project_id    INTEGER NOT NULL,
    prj_short_desc    VARCHAR(25),
    prj_required_start_date    DATE,
    prj_estimated_end_date    DATE,
    prj_actual_start_date    DATE,
    prj_actual_end_date    DATE,
    actual_salary    INTEGER NOT NULL,
    fk1_company_id    INTEGER NOT NULL,
    fk2_consultant_id    INTEGER NOT NULL,
    supervisor_name    VARCHAR(20),
    -- Specify the PRIMARY KEY constraint for table "Mch_Job".
    -- This indicates which attribute(s) uniquely identify each row of data.
    CONSTRAINT    pk_Mch_Job PRIMARY KEY (project_id)
);

lds_project

-- Create a Database table to represent the "lds_project" entity.
CREATE TABLE lds_project(
    project_id    INTEGER NOT NULL,
    pj_short_desc    VARCHAR(25),
    pj_required_start_date    DATE,
    pj_estimated_end_date    DATE,
    pj_actual_start_date    DATE,
    pj_actual_end_date    DATE,
    pj_renewal_no    INTEGER,
    pj_rate_day_proj    VARCHAR(1),
    feedback_value    INTEGER,
    actual_salary    INTEGER NOT NULL,
    fk1_account_id    INTEGER NOT NULL,
    fk2_consultant_id    INTEGER NOT NULL,
    fk3_job_role_id    INTEGER NOT NULL,
    -- Specify the PRIMARY KEY constraint for table "lds_project".
    -- This indicates which attribute(s) uniquely identify each row of data.
    CONSTRAINT    pk_lds_project PRIMARY KEY (project_id)
);

2 个答案:

答案 0 :(得分:1)

类似下面的方法应该起作用。您可以将字符串文字放入select语句中,因此通过向Union的每个部分添加不同的字符串文字,就不需要if语句。未经测试,因为我没有可以创建表和数据但应该关闭的环境。

MERGE INTO  Stage_Project s
    USING (Select project_id,pj_short_desc,pj_actual_start_date,pj_actual_end_date,pj_estimated_end_date,feedback_value, 'lds' as pj_stage_source from lds_project Union 
    Select  project_id,prj_short_desc pj_short_desc,prj_actual_start_date pj_actual_start_date ,prj_actual_end_date pj_actual_end_a,prj_estimated_end_date pj_estimate_end_date, null, 'mch' as pj_stage_source from Mch_job) u
    ON (s.Stage_Project_Key = u.project_id)
    WHEN MATCHED THEN
      UPDATE SET
    Stage_Project_Desc   = pj_short_desc,
    Stage_Estimated_End_Date  =pj_actual_start_date,
    Stage_Actual_End_Date   =pj_actual_end_date,
    Stage_FeedbackValue    =feedback_value,
    Stage_Actual_Start_Date =actual_start_date,
    Stage_LdMch_Id    =project_id,
    Stage_Source    = pj_stage_source
    WHEN NOT MATCHED THEN
      INSERT (Stage_Project_Desc,
    Stage_Estimated_End_Date,
    Stage_Actual_End_Date,
    Stage_FeedbackValue,
    Stage_Actual_Start_Date,
    Stage_LdMch_Id,
    Stage_Source)
      VALUES (pj_short_desc,pj_estimated_end_date ,pj_actual_end_date ,feedback_value,pj_actual_start_date,project_id,pj_stage_source);​

答案 1 :(得分:0)

enter image description here

在给定的图像中,错误缺少表达。 而且我还在选择语句上添加了“ ==“,因为它给出了错误(缺少等号)