If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > How to create function ?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-07-10, 06:11
bh_hensem bh_hensem is offline
Registered User
 
Join Date: Jun 2009
Posts: 18
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
Reply With Quote
  #2 (permalink)  
Old 04-07-10, 11:13
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On