Return value from MySQL stored procedure

前端 未结 4 1447
北海茫月
北海茫月 2020-12-19 08:51

So I\'ve finally decided to get around to learning how to use stored procedures, and although I do have them working, I\'m unsure if I\'m doing it correctly - aka. the b

相关标签:
4条回答
  • 2020-12-19 09:22

    your stored procedure is a little over-engineered for my liking - keep it simple :)

    MySQL

    drop table if exists tags;
    create table tags
    (
    tag_id int unsigned not null auto_increment primary key,
    name varchar(255) unique not null
    )
    engine=innodb;
    
    drop procedure if exists insert_tag;
    
    delimiter #
    
    create procedure insert_tag
    (
    in p_name varchar(255)
    )
    proc_main:begin
    
    declare v_tag_id int unsigned default 0;
    
        if exists (select 1 from tags where name = p_name) then
            select -1 as tag_id, 'duplicate name' as msg; -- could use multiple out variables...i prefer this
            leave proc_main;
        end if;
    
        insert into tags (name) values (p_name);
    
        set v_tag_id = last_insert_id();
    
        -- do stuff with v_tag_id...
    
        -- return success
        select v_tag_id as tag_id, 'OK' as msg; 
    
    end proc_main #
    
    delimiter ;
    

    PHP

    <?php
    
    ob_start(); 
    
    try{
    
        $conn = new mysqli("localhost", "foo_dbo", "pass", "foo_db", 3306);
    
        $conn->autocommit(FALSE); // start transaction
    
        // create the tag
    
        $name = 'f00';
    
        $sql = sprintf("call insert_tag('%s')", $conn->real_escape_string($name));
    
        $result = $conn->query($sql);
        $row = $result->fetch_array();
        $result->close();
        $conn->next_result();
    
        $tagID = $row["tag_id"]; //  new tag_id returned by sproc
    
        if($tagID < 0) throw new exception($row["msg"]);
    
        $conn->commit(); 
    
        echo sprintf("tag %d created<br/>refresh me...", $tagID);
    
    }
    catch(exception $ex){
        ob_clean(); 
        //handle errors and rollback
        $conn->rollback();
        echo sprintf("oops error - %s<br/>", $ex->getMessage()); 
    }
    
    // finally
    $conn->close();
    ob_end_flush();
    ?>
    
    0 讨论(0)
  • 2020-12-19 09:28

    If you want this:

    DECLARE doesTagExist BOOL;
    SET doesTagExist = CheckTagExist('str');
    

    then you should use functions:

    DELIMITER //
     CREATE FUNCTION CheckTagExists(
       tagName VARCHAR(255)
     )
     BEGIN
       DECLARE doesTagExist BOOL;
    
     -- Check if tag exists
     SELECT
      EXISTS(
       SELECT
        *
       FROM
        tags
       WHERE
        tags.NAME = tagName
      )
     INTO
      doesTagExist;
    
      RETURN doesTagExist;
    END //
    DELIMITER ;
    
    0 讨论(0)
  • 2020-12-19 09:35

    Stored PROCEDURES can return a resultset. The last thing you SELECT in a stored procedure is available as a resultset to the calling environment.. Stored FUNCTIONS can return only a single result primitive.
    You may also mark your parameters as INOUT parameters.

    0 讨论(0)
  • 2020-12-19 09:40
    DECLARE doesTagExist BOOL; 
    SET CheckTagExist('str',doesTagExist); 
    

    is the correct way of doing it with just store procedures. There are no 'regular' return values.

    0 讨论(0)
提交回复
热议问题