English 中文(简体)
• 如何定制MySQL储存功能中的错误信息
原标题:How to customize an error message in a MySQL stored function
  • 时间:2012-05-08 00:36:35
  •  标签:
  • mysql

这可能是你们大多数人面临的一个棘手问题。 以下是我在MySQL创建的储存功能:

DROP PROCEDURE IF EXISTS teamSize;
DELIMITER //

CREATE PROCEDURE teamSize(IN teamName VARCHAR(50), IN teamYear INT(4), OUT noOfPlayers INT)
BEGIN
    IF teamName IN (SELECT teamName FROM team) THEN
        SELECT COUNT(playerID) INTO noOfPlayers 
        FROM team
        JOIN teamAllocation
        ON teamAllocation.teamID = team.teamID
        WHERE team.teamName = teamName 
        AND team.teamYear = teamYear;
    ELSE
        SELECT "That team does not exist in the database" AS "Error Message";
    END IF;
END //

DELIMITER ;

我担任这一职务,因此,我可以计算某一年某个团队的规模。 团队名称和团队年按参数价值分类。 对于这一职能,如果在数据库中没有用户分类,我希望能显示用户友好的错误信息。

例如: 如我所期望的那样,在确实存在的团队中,我取得了以下成果:

mysql> CALL teamSize("U11 Orange", 2012, @noOfPlayers);
--------------
CALL teamSize("U11 Orange", 2012, @noOfPlayers)
--------------

Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @noOfPlayers;
--------------
SELECT @noOfPlayers
--------------

+--------------+
| @noOfPlayers |
+--------------+
|            2 |
+--------------+
1 row in set (0.00 sec)

mysql>

...... 但是,如果我知道某个团队中的某类人员在数据库中不存在,我会得到以下信息:

mysql> CALL teamSize("jkgefkrkvbey", 2012, @teamSize);
--------------
CALL teamSize("jkgefkrkvbey", 2012, @teamSize)
--------------

Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @teamSize;
--------------
SELECT @teamSize
--------------

+-----------+
| @teamSize |
+-----------+
|         0 |
+-----------+
1 row in set (0.00 sec)

mysql>

What I am wanting for the case where a team name parsed into the function doesn t exist is something like the following:

+------------------------------------------+
| Error Message                            |
+------------------------------------------+
| That team does not exist in the database |
+------------------------------------------+
1 row in set (0.00 sec)

mysql>

如果有人对我如何能够实现上述目标有一些反馈或建议,将会受到高度赞赏。

提前感谢!

最佳回答

我认为,你的问题是超载<代码>teamName。 由于<代码>teamName是贵程序的一个参数,以下询问:

SELECT teamName FROM team

相当于:

SELECT  foo  FROM team

始终如一地将<条码> foo 每份记录在<条码>上填报。 (因此,IF foo IN ... conditions is persistent. 你最好重新命名其参数,以便用一栏名核对。

另外,你不需要以你的方式执行<代码>IF。 相反,为什么不:

IF (SELECT COUNT(*) FROM team WHERE teamName = new_parameter_name) THEN ...

除此以外,我更希望提出一张空白卡(例如,要求一种不存在的程序),而不是退回一个显示信息的信息。

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签