Regarding database error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near if exists( select * from si_patient_casehistory WHERE patient_id = '11')

Please post the entire sql ?

below store procedure throws an syntax error while executing.

IF EXISTS (SELECT * FROM si_patient_casehistory WHERE patient_id = pid)
            UPDATE si_patient_casehistory SET case_history = _case_history WHERE patient_id = pid
            INSERT INTO si_patient_casehistory (case_history) VALUES (_case_history)

As @DarthGuido says we need to see your entire code, but

if exists( select * from si_patient_casehistory WHERE patient_id = '11')

is not the way to query a database. There are multiple errors just in that one line.

Edit: Looks like we cross-posted.

can u please make it working and give us. i am not getting actually where is the error

What scripting language are you using?

scripting language php

That doesn’t look like any PHP I’ve ever seen before…

Its Mysql

If this is your complete statement, it errors because you can not use if as a separate statement. It must always be of a create statement. If this is part of a create statement, the error is due to the code immediately preceeding the if statement.If this is the case, post the code for the create statement.

Your if statement is also not correct. You are missing the keyword THEN and that statements should be terminated with a semicolon.

from the looks of what OP seems to be trying to do, i think a simple INSERT with ON DUPLICATE KEY might be the solution


please can u correct it give us…

I’m sorry but I don’t understand what you are trying to say with the latest post.

we are getting error while executing in below query, can u give correct and give me.

IF EXISTS (SELECT * FROM si_patient_psy_emr_medhistory WHERE patient_id = 5)
UPDATE si_patient_psy_emr_medhistory SET symptom = ‘123’ WHERE patient_id = 5
INSERT INTO si_patient_psy_emr_medhistory (symptom) VALUES (‘123’);

As I’ve already stated, if that is your complete query it will not work as it is not possible to use an if statement outside a create procedure or a create trigger statement. If it is part of a procedure ot trigger definition you are missing the semicolon after the update and the end if as well.

As R937 pointed you can accomplish this by using the ON DUPLICATE clause for insert. See the mysql documentation for more details.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.