简体   繁体   中英

how do i write a stored procedure for my code in mysql

not sure how to write stored procedure for the php function below. looking to write a stored procedure that will check for duplication in a range.

function check_for_duplication($job_no) {
        global $connection;


        $query = "SElECT COUNT (*) ";
        $query .= "FROM jobs ";
        $query .= "WHERE job_no = {$job_no}"; 
        $query .= "AND start <= {$new_end}";
        $query .= "AND end >= {$new_start}";
        $job_set = mysqli_query($connection, $query);  
        confirm_query($job_set);
        if($job = mysqli_num_rows($job_set)) {
        return $job_set;
        } else {
        return null;
      }
     }

You might try something like this.

DROP FUNCTION IF EXISTS check_for_duplication
CREATE FUNCTION check_for_duplication
(varJobNo IN datatype, 
 varNewEnd IN datatype,
 varNewStart IN datatype)
RETURNS CURSOR
BEGIN
    // this is where we declare our variables
    DECLARE var_cursor CURSOR;

    // begin logic
    SELECT COUNT (*) INTO var_cursor
    FROM jobs
    WHERE job_no=varJobNo AND
      start<=varNewEnd AND
      end>=varNewStart;

    RETURN var_cursor;
END;

This is just a basic function that will still require you to do some of the logic in your php but you can adjust the function to do more. I know there is a way to use a stored procedure to return data but it is easier for me to do it this way. Functions are for returning data while a procedure is to perform some action without returning anything. I hope this helps you out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM