如果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吗?
答案 0 :(得分:0)
您可以使用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列。
答案 1 :(得分:0)
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
答案 2 :(得分:0)
如果您想编写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();
}
答案 3 :(得分:0)
根据您的要求更改变量数据类型
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