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

  • Home
  • SEARCH
  • 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 3304454
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:03:49+00:00 2026-05-17T21:03:49+00:00

I have just started to create a stored function this is my first time

  • 0

I have just started to create a stored function this is my first time so I am having a few problems. Currently I call the function using SELECT test(); (test is the function name for now). I want to send a number to the function (username ID) and have the username returned.

I have this working by using SELECT test(1); 1 is the ID of a user in the table. This seems to work as the username is returned, but if I type in any number the same username is returned also.

BEGIN 

 DECLARE new_username VARCHAR(90);    

    SELECT `username` INTO  new_username FROM `users` WHERE `ID` = ID;

 return new_username;
END

I have set the paramter as ID int .

Am I right in thinking that the keyword INTO will put the value of the username into the variable new_username ? If I run it without the INTO I get the error:

Not allowed to return a result set from a function

Have I made any obvious mistakes in this, I hope I havent done it totally wrong. Thanks for any advice :).

Edit : I just added a few more rows into my table , I now get the error:

Result consisted of more than one row

Full sql version:

CREATE DEFINER=`elliotts`@`%` FUNCTION `test`(ID int) 
RETURNS varchar(32) CHARSET latin1
BEGIN 

    DECLARE new_username VARCHAR(32);

    SELECT `username` 
      INTO new_username 
      FROM `users` 
     WHERE `ID` = ID;

    return new_username;

END
  • 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-05-17T21:03:49+00:00Added an answer on May 17, 2026 at 9:03 pm

    Use:

    DROP FUNCTION IF EXISTS `example`.`test` $$
    CREATE FUNCTION `example`.`test` (param INT) RETURNS VARCHAR(32)
    BEGIN
    
      DECLARE new_username VARCHAR(32);
    
        SELECT `username`
          INTO new_username
          FROM `users`
         WHERE `ID` = param;
    
        RETURN COALESCE(new_username, 'Username not found');
    
    END $$
    

    Mind that the VARCHAR length of the RETURN value matches the variable, which should match the column length you want to return.

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

Sidebar

Related Questions

Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have just started using silverlight 2 beta and cannot find how to or
I have just started using Boost 1.36. These libraries would be very useful in
I have just started learning Erlang and am trying out some Project Euler problems
I have just started playing with the ASP.Net MVC framework, and today I created
Have just started playing with ASP.NET MVC and have stumbled over the following situation.
I have just started to study computer sciences at my university where they teach
I have just started to look at .NET 3.5 so please forgive me if
I have just started migrating my homegrown persistence framework to JPA. Given that the

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.