Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

MySql 5 Functions

  • 21-07-2009 05:01PM
    #1
    Registered Users, Registered Users 2 Posts: 302 ✭✭


    I am tearing out what is left of my hair on this problem.

    I am using MySql Server version : 5.0.45-community-nt and I am attempting to write a stored function. I have tried a clatter of examples from the web but none of them will work for me. Can anyone suggest what I might be doing wrong?
    DELIMITER ;
    CREATE FUNCTION test ( input_int int )
    RETURNS int
    BEGIN
    DECLARE var_returned int;
    IF input_int > 20 THEN
    SET var_returned = 50;
    END IF;
    return var_returned;
    END;
    

    I get the following message

    You have an error in your SQL syntax; check the manual that corresponds to your MySql server version for the right syntax to use near '' at line 4.

    Ultimatly I want a function that will take a string and return a blank string if the parameter id null. e.g.
    DELIMITER ;
    CREATE FUNCTION convertNull ( input_str VARCHAR(200) )
    RETURNS VARCHAR(200)
    BEGIN
       DECLARE result_str VARCHAR(200);
       SET result_str  = input_str;
       IF isNull(result_str)  THEN
          SET result_str  = "";
       END IF;
       return result_str;
    END;
    


Comments

  • Registered Users, Registered Users 2, Paid Member Posts: 2,032 ✭✭✭lynchie


    Does the following work?
    DELIMITER $$
    CREATE FUNCTION test ( input_int int )
    RETURNS int
    BEGIN
    DECLARE var_returned int;
    IF input_int > 20 THEN
    SET var_returned = 50;
    END IF;
    return var_returned;
    END$$
    


  • Registered Users, Registered Users 2 Posts: 302 ✭✭BlueSpud


    No joy, it gives the following error:
    [SIZE=1]Error Code : 1064[/SIZE]
    [SIZE=1]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF input_int > 20 THEN[/SIZE]
    [SIZE=1]SET var_returned = 50;[/SIZE]
    [SIZE=1]END IF;[/SIZE]
    [SIZE=1]return var_returned;[/SIZE]
    [SIZE=1]END$$' at line 1[/SIZE]
    [SIZE=1](0 ms taken)[/SIZE]
     
    


  • Registered Users, Registered Users 2, Paid Member Posts: 2,032 ✭✭✭lynchie


    Strange, it works fine for me on mysql 5.0.67
    mysql> drop function test;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> DELIMITER $$
    mysql> CREATE FUNCTION test ( input_int int )
        -> RETURNS int
        -> BEGIN
        -> DECLARE var_returned int;
        -> IF input_int > 20 THEN
        -> SET var_returned = 50;
        -> END IF;
        -> return var_returned;
        -> END$$
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> exit
    


  • Registered Users, Registered Users 2 Posts: 302 ✭✭BlueSpud


    You are using the command prompt, however, I am using SQLyog as a front end, as I will be running the scripts against a number of databases.


Advertisement