Jump to content

Hello dear tech community I'm seeking some light on my SQL I have been tasked to create a procedure for my database, I have been provided with code I have to fill in, put it make no sense because every tutorial I have looked at is completely different. 

DELIMITER $$

CREATE PROCEDURE course_scheduler (IN new_code CHAR(3), IN start_date DATE)
  BEGIN
    DECLARE isExisting BOOLEAN DEFAULT FALSE;
    DECLARE new_code CHAR(3);
    DECLARE start_date CURSOR FOR
      SELECT course_code FROM module WHERE new_code = course_code;
    DECLARE CONTINUE HANDLER FOR NOT FOUND
      SET isExisting = TRUE;

    -- NOTE : check if course exists !
    IF (isExisting) THEN
      SIGNAL SQLSTATE '45000'
        SET MESSAGE_TEXT = ' Course exists';
    END IF;

    -- NOTE : check if start date is >= month in future !
    IF (start_date) THEN
      SIGNAL SQLSTATE '45000'
        SET MESSAGE_TEXT = ' Course starts';
    END IF;

    OPEN todo;

    todo: LOOP
      FETCH NEXT FROM todo INTO todo;

      IF (todo) THEN
        LEAVE todo;
      END IF;

      -- NOTE : Check Saturday / Sunday & Skip using WEEKDAY & DATE_ADD functions respectivly in MySQL . . Google !

      INSERT INTO `session` (`code`, `date`, room)

      SET start_date = DATE_ADD(CURRENT_DATE, INTERVAL 1 MONTH);
    END LOOP;

    CLOSE todo;
  END$$

DELIMITER ;

 

the word "todo" needs to be replaced with the appropriate variable from the database, but am just not getting it. 

 

https://imgur.com/a/1QZYdUi : database

Can't add the screenshot of the databse for some reason. 

*Fells Stupid* 

 

Any help is much apreaciated 

 

EDIT: 

o    Produce a procedure that assigns a new schedule for a given course. The procedure should accept a course code & start date as arguments then schedule each of the given course’s modules on consecutive (working / week) days. The start date must be at least a month in the future otherwise, a suitable error should be raised.

o    Tips . . CURSOR, LOOP, SIGNAL

Link to comment
https://linustechtips.com/topic/1118163-mysql-procedures-stuck/
Share on other sites

Link to post
Share on other sites

It would help if you could provide the basic logic for what you need this procedure to do, but from what I gather it aims to check for courses in the Session table that exist and whether this course is in a future month, along with a few other checks for day of the week, etc. Why is this procedure needed? What good is using a cursor here? Seems like this is a simple enough operation performed on a simple dataset, using a cursor is probably overthinking it. Cursors are good for many niche calculations and operations, but once you begin to use them you disregard the set-based logic of a database. If you take the logic of what you need this procedure to do, you can most likely build a more reasonable set-based solution for this.

Link to comment
https://linustechtips.com/topic/1118163-mysql-procedures-stuck/#findComment-13003160
Share on other sites

Link to post
Share on other sites

1 hour ago, APileofRocks said:

It would help if you could provide the basic logic for what you need this procedure to do, but from what I gather it aims to check for courses in the Session table that exist and whether this course is in a future month, along with a few other checks for day of the week, etc. Why is this procedure needed? What good is using a cursor here? Seems like this is a simple enough operation performed on a simple dataset, using a cursor is probably overthinking it. Cursors are good for many niche calculations and operations, but once you begin to use them you disregard the set-based logic of a database. If you take the logic of what you need this procedure to do, you can most likely build a more reasonable set-based solution for this.

This is the task I have been assigned and I'm struggling inclemency, I understand the logic more or less but don't understand how to implement it. After implementing a procedure I need to develop a trigger then connect it to netbeans to make it fully functional. 

Link to comment
https://linustechtips.com/topic/1118163-mysql-procedures-stuck/#findComment-13003319
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×