Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7897265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:07:22+00:00 2026-06-03T08:07:22+00:00

This is probably a trivial problem for most of you… Below is a stored

  • 0

This is probably a trivial problem for most of you… Below is a stored function I have created in 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 ;

I made this function so I can calculate the size of a given team in a given year. The team name and the team year are parsed in as parameter values. For this function I want a user friendly error message to display if the team name the user typed in does not exist in the database.

For example: I type in a team that does exist I get the following results, as I expected:

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>

…But if I type in a team that I know doesn’t exist in the database I get the following:

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>

If anyone has some feedback or advice on how I can achieve the above that would be very much appreciated.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:07:23+00:00Added an answer on June 3, 2026 at 8:07 am

    I think your problem is the overloading of the term teamName. Since teamName is a parameter to your procedure, the following query:

    SELECT teamName FROM team
    

    Is equivalent to:

    SELECT 'foo' FROM team
    

    Which will always return the string 'foo' once for each record in team (and therefore the IF 'foo' IN ... condition is always true). You would be well-advised to rename your parameter so that it doesn’t collide with a column name.

    Also, you need not perform the IF in the manner you are. Instead, why not:

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

    As an aside, I prefer to raise a SQL error (e.g. by calling a non-existent procedure) rather than return a message string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Probably a very trivial problem. I have an object that looks like this: @PersistenceCapable
This is probably trivial to most of you, but I haven't been writing stored
This is probably a trivial question for most but here goes... I am using
For someone who knows Regular Expressions, this is probably pretty trivial. I have several
Strange and probably trivial problem. I have three projects in one solution (.NET 2.0,
This is a slightly odd problem, which means the solution is probably something trivial
This problem is probably trivial for 99% of you. So far I always tried
This is probably trivial, and I do have a solution but I'm not happy
I'm new and this is probably very trivial but I have the following method
I have done a fair amount of searching for this problem, which is probably

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.