0

I am working on a procedure which is doing the following: With a cursor declared I want to go through all entries of a table to find entries where the given period is covering the one from the cursor.

Example: a student A went to school at grade x from date 3 to date 6. Let's find all who studied with him at least one time for the same class.

The while-loop would be applied to the same table. But I want to just find out if there is at least one entry or not. So the while loop should stop if the first entry appears.

Example: A student B could have visited the same class as A did. But for a year he has been in another class. Just for example.

And here is my problem. I have two variables to set and I get a syntax error.

DELIMITER $$
DROP PROCEDURE IF EXISTS coworkers$$

CREATE PROCEDURE coworkers(
IN gus INT, 
IN rus INT,
OUT gto INT 

)
BEGIN
    DECLARE recCID INT;
    DECLARE recSDT DATE;
    DECLARE recEDT DATE;
    DECLARE done INT DEFAULT FALSE;

    DECLARE ctr INT;



    DECLARE cwrk CURSOR FOR
        SELECT comp_id, start_date, end_date FROM skill_cv_test WHERE usr_id = rus;
    DECLARE CONTINUE HANDLER 
        FOR NOT FOUND
        SET done = TRUE;

    OPEN cwrk;
    SET ctr = 0;
    loop_cwrk: WHILE(ctr<1) DO 

        FETCH cwrk INTO recCID, recSDT, recEDT;

        IF EXISTS
        (SELECT *
        FROM skill_cv_test AS m
        WHERE m.usr_Id = gus AND m.usr_id != rus AND (m.start_date < recSDT OR m.end_date <= recEDT) AND m.comp_id = recCID)
        THEN
        SET ctr = 1,
        SET gto = 1;


        IF done THEN
            LEAVE loop_cwrk;
        END IF;



    END WHILE loop_cwrk;
    CLOSE cwrk;

end $$
delimiter ;
CALL coworkers(2,1,@gto);  

I have tried different formats for the section THEN SET... which is the problem. Here is the error code:

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 ''ctr' := 1, SET 'gto' ;= 1;

Here you can see what else I have tried (among others). Probably it's a totally wrong approach.

Btw: the Select within the loop is only a placeholder to get the whole procedure work.

But now, what am I doing wrong?

Thanks in advance.

SOLUTION

DELIMITER $$
DROP PROCEDURE IF EXISTS coworkers$$

CREATE PROCEDURE coworkers(
IN gus INT, 
IN rus INT,
OUT gto INT 

)
BEGIN
    DECLARE recCID INT;
    DECLARE recSDT DATE;
    DECLARE recEDT DATE;
    DECLARE done INT DEFAULT FALSE;

    DECLARE ctr INT;



    DECLARE cwrk CURSOR FOR
        SELECT comp_id, start_date, end_date FROM skill_cv_test WHERE usr_id = rus;
    DECLARE CONTINUE HANDLER 
        FOR NOT FOUND
        SET done = TRUE;

    OPEN cwrk;
    SET ctr = 0;
    loop_cwrk: WHILE(ctr<1) DO 

        FETCH cwrk INTO recCID, recSDT, recEDT;

        IF EXISTS
           (SELECT *
            FROM skill_cv_test AS m
              WHERE m.usr_Id = gus AND m.usr_id != rus AND (m.start_date < recSDT 
             OR m.end_date <= recEDT) AND m.comp_id = recCID)
          THEN
            SET ctr = 1;
            SET gto = 1;
        END IF;


        IF done THEN
            LEAVE loop_cwrk;
        END IF;



    END WHILE loop_cwrk;
    CLOSE cwrk;

end $$
delimiter ;
CALL coworkers(2,1,@gto); 

1 Answer 1

1

You need add END IF for your first IF

    IF EXISTS
       (SELECT *
        FROM skill_cv_test AS m
          WHERE m.usr_Id = gus AND m.usr_id != rus AND (m.start_date < recSDT 
         OR m.end_date <= recEDT) AND m.comp_id = recCID)
      THEN
        SET ctr = 1;
        SET gto = 1;
    END IF; -- Add END IF here


    IF done THEN
        LEAVE loop_cwrk;
    END IF;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. May I ask is there something more to consider with symbols? Seems like there's another issue.
@nucky Sure,you can update your question with the new issue
My fault: I missed a semicolon after the first SET it seems.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.