Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2009
    Posts
    18

    Unanswered: How to create function ?

    Hi..

    Below is my code to create function called get_first_record and get_last record.

    Code:
    CREATE FUNCTION get_first_record (tab varchar(20))
    RETURNS VARCHAR(20)
    BEGIN
    RETURN select FieldID9 from tab order by FieldID1 asc limit 1;
    END;
    
    CREATE FUNCTION get_last_record (tab varchar(20))
    RETURNS VARCHAR(20)
    BEGIN
    RETURN select FieldID9 from tab order by FieldID1 desc limit 1;
    END;
    But i got an error as below:-
    MySQL Database Error: 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 'select FieldID9 from tab order by FieldID1 asc limit 1;
    END' at line 4

    Could somebody help me ?..

    Thank you,
    Baharin

  2. #2
    Join Date
    Mar 2004
    Posts
    480
    If you are creating this in the mysql client you will get an error if you don't change the delimiter (the character used to denote an end of commands, by default this is semi-colon).

    Try wrapping your query like this:

    Code:
    DELIMITER $$
    CREATE FUNCTION get_first_record (tab varchar(20))
    RETURNS VARCHAR(20)
    BEGIN
    RETURN select FieldID9 from tab order by FieldID1 asc limit 1;
    END
    $$
    DELIMITER ;
    and see if that is the problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •