简体   繁体   English

我如何在mysql中为我的代码编写存储过程

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

not sure how to write stored procedure for the php function below. 不知道如何为下面的php函数编写存储过程。 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. 这只是一个基本功能,仍然需要您在php中执行一些逻辑,但是您可以调整该功能以执行更多操作。 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. 我希望这能够帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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