sql - MySQL 存储过程的返回值

标签 sql mysql stored-procedures

所以我最终决定开始学习如何使用存储过程,虽然我确实在使用它们,但我不确定我是否正确地使用它 - 也就是。 最好的方式。这就是我所拥有的。

三个过程:TryAddTag、CheckTagExists 和 AddTag。

TryAddTag 是我在其他代码(例如 PHP 等)和其他两个过程之间的中介过程,所以这是一个被调用的过程。


尝试添加标签

DELIMITER //
 CREATE PROCEDURE TryAddTag(
  IN tagName VARCHAR(255)
 )

 BEGIN

 -- Check if tag already exists
 CALL CheckTagExists(tagName, @doesTagExist);

 -- If it does not exist, add it
 IF @doesTagExist = FALSE THEN
  CALL AddTag(tagName);
 END IF;

END //
DELIMITER ;


添加标签

DELIMITER //
 CREATE PROCEDURE AddTag(
  IN tagName VARCHAR(255)
 )
 BEGIN

 INSERT INTO
  tags
 VALUES(
  NULL,
  tagName
 );

END //
DELIMITER ;


CheckTagExists

DELIMITER //
 CREATE PROCEDURE CheckTagExists(
  IN
   tagName VARCHAR(255),
  OUT
   doesTagExist BOOL
 )
 BEGIN

 -- Check if tag exists
 SELECT
  EXISTS(
   SELECT
    *
   FROM
    tags
   WHERE
    tags.NAME = tagName
  )
 INTO
  doesTagExist;

END //
DELIMITER ;


我的问题源于此和@doesTagExist 的使用。

-- Check if tag already exists
CALL CheckTagExists(tagName, @doesTagExist);

是使用其中一个变量的正确方法吗?和/或,如何使用 DECLARE 变量将 CheckTagExists 的结果存储在 TryAddTag 中?我期待一些类似的东西

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

或者类似的东西......

最佳答案

根据我的喜好,您的存储过程有点过度设计 - 保持简单:)

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();
?>

关于sql - MySQL 存储过程的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4362722/

相关文章:

MySQL 喜欢不喜欢 `\` ?

stored-procedures - 带有存储过程调用的 BEFORE INSERT 触发器 (DB2 LUW 9.5)

sql - MySQL + MyISAM 中的 "System lock"

mysql - 什么是呈现时间序列数据的首选方法

mysql - SQL 和外键,引用

mysql - 在 MySQL 存储过程中使用参数 WHERE-CLAUSE 会降低性能

sql-server - 如何回滚存储过程中的事务?

sql - 有关 EXCEL 中的 SQL 和 Microsoft OLE DB Provider for Jet 4.0 的任何引用/手册吗?

java - 如何在 Derby 数据库中设置双列的精度?

mysql - 在 Snow Leopard 上启动 MySQL 错误