0

因此,我正在尝试使用我的 select 语句中传入的 FName 和 LName 参数来设置变量,从我之前在存储过程中添加到数据库中的患者那里获取 PatientID(主键,非空,自动增量),但我收到语法错误。因此,我们将不胜感激有关语法失败原因的任何帮助。

下面是相关代码,pFName和pLName是传入存储过程的参数。

DECLARE pPolicyHolder INT;
SET pPolicyHolder = (SELECT PatientID FROM Patient WHERE Fname = pFName AND LName = pLName);

这是整个过程。我意识到这可能不是最好/最干净的方法,但是设置表格的方式是需要在添加患者之前添加 MedicalHistory,并且必须在添加 PatientInsurance 之前添加患者,因为外键约束。

DELIMITER //
CREATE PROCEDURE AddNewPatient(
    IN pAllergies TEXT, IN pMedications TEXT, IN pExistingConditions TEXT, IN pMisc       
    TEXT, IN pFName VARCHAR(30), IN pLName VARCHAR(45), IN pGender CHAR(1), IN pDOB 
    DATE, IN pSSN DOUBLE, IN pMedicalHistory INT, IN pPrimaryPhysician INT, IN 
    pInsuranceCompany INT, IN pCoPay INT)
BEGIN
    START TRANSACTION;
         INSERT INTO MedicalHistory(Allergies, Medications, ExistingConditions, Misc) 
         VALUES(pAllergies, pMedications, pExistingConditions, pMisc);
    COMMIT;
        START TRANSACTION;
              INSERT INTO Patient(FName, LName, Gender, DOB, SSN, 
              MedicalHistory,PrimaryPhysician) VALUES(pFName, 
              LName,pGender,pDOB,pSSN,pMedicalHistory,pPrimaryPhysician);
        COMMIT;

       DECLARE pPolicyHolder INT;
       SET pPolicyHolder = (SELECT PatientID FROM Patient WHERE Fname = pFName AND 
                            LName = pLName);
    START TRANSACTION;
       INSERT INTO PatientInsurance(PolicyHolder, InsuranceCompany, CoPay) 
       VALUES(pPolicyHolder, pInsuranceCompany, pCoPay);
    COMMIT;
    END //
DELIMITER ;

错误:

ERROR 1064 (42000): 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 'DECLARE pPolicyHolder INT;
   SELECT PatientID into pPolicyHolder FROM Patient W' at line 15
4

1 回答 1

2

您需要在过程开始时声明变量,而不是在中间。

CREATE PROCEDURE AddNewPatient(...)
BEGIN
   DECLARE pPolicyHolder INT;  <------declare here

   START TRANSACTION;
   ...
   COMMIT;

   SET pPolicyHolder = (SELECT PatientID FROM Patient WHERE Fname = pFName AND 
                        LName = pLName);
   ...
于 2013-04-14T15:19:22.547 回答