CREATE DEFINER=`root`@`%` PROCEDURE `createCow`(IN inputAnimalId BIGINT, IN inputFarmId INT, IN inputTransferDate DATE) BEGIN DECLARE number INT; DECLARE isTransfered INT;
SELECT COUNT(*) INTO number FROM cow WHERE `animalID` = inputAnimalId;
IF number = 0 THEN INSERT INTO cow (`animalID`) VALUES (inputAnimalId); INSERT INTO Farm_has_cow (`Farm_FarmId`, `cow_animalID`, `cowTransferDate`, `cowLiveHere`) VALUES (inputFarmId, inputAnimalId, inputTransferDate, 1); ELSE SELECT `cowLiveHere` AS thisCowLiveHere, `cowTransferDate` AS thisCowTransferDate, COUNT(*) INTO isTransfered FROM Farm_has_cow WHERE `Farm_FarmId` = inputFarmId AND `cow_animalId` = inputAnimalId AND `cowLiveHere` = 1 GROUP BY `cowLiveHere`;
IF isTransfered = 1 AND inputTransferDate < thisCowTransferDate THEN UPDATE Farm_has_cow SET `cowTransferDate` = inputTransferDate WHERE `cow_animalID` = inputAnimalId AND `Farm_farmId` = inputFarmId; ELSE UPDATE Farm_has_cow SET `cowLiveHere` = 0 WHERE `cow_animalID` = inputAnimalId AND `Farm_farmId` = inputFamId AND thisCowLiveHere = 1; INSERT INTO Farm_has_cow (`Farm_FarmId`, `cow_animalID`, `cowTransferDate`, `cowLiveHere`) VALUES (inputFarmId, inputAnimalId, inputTransferDate, 1); END IF; END IF; END
Som du kan se er der ingen union.. Har du andre forslag?
SELECT `cowLiveHere` AS thisCowLiveHere, `cowTransferDate` AS thisCowTransferDate, COUNT(*) INTO isTransfered FROM Farm_has_cow WHERE `Farm_FarmId` = inputFarmId AND `cow_animalId` = inputAnimalId AND `cowLiveHere` = 1 GROUP BY `cowLiveHere`;
Fandt ud af at hvis jeg laver dem med INTO skal der self stå som: tbl1, tbl2 INTO val1, val2 FROM... istedet, så nu virker det :)
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.