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 9073157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:21:01+00:00 2026-06-16T18:21:01+00:00

Okay, so this is my first time attempting to CREATE FUNCTION , so please

  • 0

Okay, so this is my first time attempting to CREATE FUNCTION, so please bear with me!

So, I have this (simplified) table structure:

CREATE TABLE `pokemon` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `userid` INT UNSIGNED NOT NULL,
    `boxid` TINYINT UNSIGNED NOT NULL DEFAULT 255,
    `boxpos` SMALLINT UNSIGNED,
    PRIMARY KEY (`id`),
    UNIQUE KEY `position` (`userid`,`boxid`,`boxpos`)
) ENGINE=InnoDB

I want to create a function that, given a user ID and box ID, returns an empty box position (ie. one that won’t trigger a duplicate key error).

Note that boxid uses values from 0 to 249 for box IDs, and 255 as a special value for the user’s Party. boxpos can range from 0 to 45,504 in Boxes, but only 0 through 5 in the Party. Also, in the Party the returned position should be the first empty one, whereas in a Box it should be a random position.

So, with all this in mind, here was my attempt:

begin
declare ret smallint unsigned;
declare exist tinyint unsigned default 1;
if fieldid = 255 then
  create temporary table `party` (
    `pos` smallint unsigned not null
  );
  insert into `party` values (0),(1),(2),(3),(4),(5);
  delete from `party` where `pos` in (select `fieldpos` from `pokemon` where `userid`=userid and `fieldid`=255);
  select `pos` into ret from `party` limit 1;
  if ret is null then select `[[Error: No room in Party]]` from `party`; end if;
else
  while exist=1 do
    set exist=0;
    set ret=floor(rand()*45504);
    select 1 into exist from `pokemon` where `fieldid`=fieldid and `userid`=userid and `fieldpos`=ret;
  end while;
end if;
return ret;
end;

(Note that this is the body of the function, as entered into phpMyAdmin)

EDIT: I’ve fixed the DECLARE problem, but now it’s saying I can’t return a result set from a function.

I’m unsure where I’ve gone wrong, and I think I need help getting on the right track. In particular, have I got the function logic right in the first place?

  • 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-16T18:21:02+00:00Added an answer on June 16, 2026 at 6:21 pm

    In this line:

    if ret is null then select `[[Error: No room in Party]]` from `party`; end if;
    

    you select a constant value for all rows from the party table but don’t put the result of that select into a variable. That’s probably where the error comes from.

    It should probably be something like:

    if ret is null then 
       set ret = '[[Error: No room in Party]]';
    end if;
    

    (Also note that String literals need to be enclosed in single quotes ('), not with those dreaded backticks – which aren’t necessary in the first place, so it’s better to leave them out alltogether).

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

Sidebar

Related Questions

This is my first time implementing Dijkstra's algorithm. Okay so here I have a
Okay, so this is not the first time I've had this problem, but it
Okay, this is like the 5th time I have had to ask this question,
Okay this is my first post here so please forgive me if i mess
Okay this is my first WPF app and I am having a hard run
Okay at first I thought this would be pretty straightforward. But I can't think
Okay this question is very simple: I have a facebook page, and a website.
Okay this may be a simple question but I have yet to come with
Okay - this is the dumbest glitch I have seen in a while: <!DOCTYPE
I have one problem with semaphores, I though you could help me.. Okay first

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.