Is there a more efficient way of doing this? I do not want to update more than one record and it is possible that the two initial queries can match two different records.
SET @AppID = (SELECT appID FROM employees WHERE ssn = vSSN);
IF @AppID IS NULL THEN
SET @AppID = (SELECT appID from applications WHERE appDate = vDate);
END IF;
UPDATE applications
SET status = vStatus
WHERE appID = @AppID;
You can use the COALESCE operator and the UPDATE/REPLACE statement. To be more precise, COALESCE picks the first non-null value out of a comma-separated list.
http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html#function_coalesce
http://dev.mysql.com/doc/refman/5.5/en/update.html
http://dev.mysql.com/doc/refman/5.5/en/replace.html
First, the UPDATE alternative:
Second, the REPLACE alternative. Please note that REPLACE first deletes the old entry and then reinserts the new one. Therefore, employees.appID may not be a foreign key of applications.appID.