【问题标题】:I want to update a row if Ptnt_id already exists if not add new one如果不添加新的 Ptnt_id 已经存在,我想更新一行
【发布时间】:2015-11-13 22:59:50
【问题描述】:

如果 Ptnt_id 已经存在,我想更新一行,如果不添加新行

IF EXISTS (select * from `tbl_medicalhistory` where `Ptnt_id` =0) THEN
    update `tbl_medicalhistory` set `txt_tongue`= 'UPDATED' where `Ptnt_id` = 0;
  ELSE 
    INSERT INTO `tbl_medicalhistory`(`idMed`, `Ptnt_id`, `txt_tongue`, `txt_palate`, `txt_tonsil`, `txt_lips`, `txt_floorOfMouth`, `txt_cheeks`, `txt_allergy`, `txt_HeartDisease`, `txt_BloodDyscracia`, `txt_Diabetes`, `txt_kidney`, `txt_liver`, `txt_hygiene`, `txt_others`) VALUES (20],20,"This","New","Table","","","","","","","","","","","OTHERS");

idMed 将是主键,Ptnt_id 将是外键,所以如果 Ptnt_id 已经存在,它将只更新整行,否则它将添加具有新 idMed 和 Ptnt_id 的另一行 有人可以帮我写那个QUERY/PROCEDURE吗?

【问题讨论】:

  • 使用 MySQL 的 INSERT ... ON DUPLICATE KEY UPDATE 语法。 Doc

标签: c# mysql sql xampp


【解决方案1】:

您可以使用INSERT ... ON DUPLICATE KEY UPDATE 语法。

上述查询的简化版本如下所示:

INSERT INTO `tbl_medicalhistory`(`idMed`, `Ptnt_id`, `txt_tongue`) 
VALUES (20,20,"This")

ON DUPLICATE KEY UPDATE `txt_tongue`="That";

鉴于 Ptnt_id 是 UNIQUE 或 PRIMARY 索引,将插入 Ptnt_id=20 不存在的行,否则如果该行存在,则更新其 txt_tongue 列。

Documentation

【讨论】:

  • idMed 将是主键,Ptnt_id 将是外键,因此如果 Ptnt_id 已经存在,它将只更新整行,否则它将添加具有新 idMed 和 Ptnt_id 的另一行
  • 那么Ptnt_id不是索引?你能发布SHOW CREATE TABLE tbl_medicalhistory的输出吗?
  • 无法发布图片,因为这里还是新的 :(
  • 您可以更新问题并将文本输出粘贴到那里。
【解决方案2】:

CREATE PROCEDURE [dbo].[PROCEDURE_NAME]
(
@Ptnt_id int
)
BEGIN
IF EXISTS (SELECT * FROM [dbo].[tbl_medicalhistory] WHERE Ptnt_id=@Ptnt_id)
BEGIN
UPDATE [dbo].[tbl_medicalhistory]
SET txt_tongue= 'UPDATED'
WHERE Ptnt_id=@Ptnt_id
END
ELSE
BEGIN
INSERT INTO [dbo].tbl_medicalhistory(idMed, Ptnt_id, txt_tongue, txt_palate, txt_tonsil, txt_lips, txt_floorOfMouth, txt_cheeks, txt_allergy, txt_HeartDisease, txt_BloodDyscracia, txt_Diabetes, txt_kidney, txt_liver, txt_hygiene, txt_others) VALUES (20,@Ptnt_id,"This","New","Table","","","","","","","","","","","OTHERS")
END
END

【讨论】:

    【解决方案3】:

    如果您想编写 c# 编码,这可能会对您有所帮助

    patient_id="123"; 
    string str1="";  
    SqlConnection con=new SqlConnection(constr);
    con.Open();
    SqlCommand cmd=new SqlCommand("SELECT ptnt_id FROM TABLE  ", con);
    SqlDataReader dr=cmd.ExecuteReader();
    while(dr.Read())
    {
    string pid=dr[0].toString();
      if(patient_id == pid)
       {
         str1="true";
       }
    
    }
    
    if(str1 == true)
    {
    con.close();
    con.open();
    SqlCommand cmd1=new SqlCommand("UPDATE TABLE SET ..... WHERE ptnt_id='"+pid+"' ",con);
    cmd.executeNonQuery();
    }
    else
    {
    con.close();
    con.open();
    SqlCommand cmd1=new SqlCommand("INSERT INTO TABLE VALUES .... ",con);
    cmd.executeNonQuery();
    

    }

    【讨论】:

      【解决方案4】:

      根据您的要求更改变量数据类型

      CREATE PROCEDURE [dbo].[PROCEDURE_NAME] (
          @Ptnt_id INT
          ,@idMed INT
          ,@Ptnt_id INT
          ,@txt_tongue NVARCHAR(50)
          ,@txt_palate NVARCHAR(50)
          ,@txt_tonsil NVARCHAR(50)
          ,@txt_lips NVARCHAR(50)
          ,@txt_floorOfMouth NVARCHAR(50)
          ,@txt_cheeks NVARCHAR(50)
          ,@txt_allergy NVARCHAR(50)
          ,@txt_HeartDisease NVARCHAR(50)
          ,@txt_BloodDyscracia NVARCHAR(50)
          ,@txt_Diabetes NVARCHAR(50)
          ,@txt_kidney NVARCHAR(50)
          ,@txt_liver NVARCHAR(50)
          ,@txt_hygiene NVARCHAR(50)
          ,@txt_others NVARCHAR(50)
          )
      
      BEGIN
          IF EXISTS (
                  SELECT 1
                  FROM [dbo].[tbl_medicalhistory]
                  WHERE Ptnt_id = @Ptnt_id
                  )
          BEGIN
              UPDATE [dbo].[tbl_medicalhistory]
              SET txt_tongue = 'UPDATED'
              WHERE Ptnt_id = @Ptnt_id
          END
          ELSE
          BEGIN
              INSERT INTO [dbo].tbl_medicalhistory (
                  idMed
                  ,Ptnt_id
                  ,txt_tongue
                  ,txt_palate
                  ,txt_tonsil
                  ,txt_lips
                  ,txt_floorOfMouth
                  ,txt_cheeks
                  ,txt_allergy
                  ,txt_HeartDisease
                  ,txt_BloodDyscracia
                  ,txt_Diabetes
                  ,txt_kidney
                  ,txt_liver
                  ,txt_hygiene
                  ,txt_others
                  )
              VALUES (
                  20
                  ,@Ptnt_id
                  ,@idMed
                  ,@Ptnt_id
                  ,@txt_tongue
                  ,@txt_palate
                  ,@txt_tonsil
                  ,@txt_lips
                  ,@txt_floorOfMouth
                  ,@txt_cheeks
                  ,@txt_allergy
                  ,@txt_HeartDisease
                  ,@txt_BloodDyscracia
                  ,@txt_Diabetes
                  ,@txt_kidney
                  ,@txt_liver
                  ,@txt_hygiene
                  ,@txt_others
                  )
          END
      END
      

      【讨论】:

        猜你喜欢
        • 2021-01-04
        • 1970-01-01
        • 2020-07-08
        • 2014-11-22
        • 2014-07-04
        • 1970-01-01
        • 1970-01-01
        • 2014-03-01
        • 2017-07-22
        相关资源
        最近更新 更多