Unix Technical Forum

issues creating function

This is a discussion on issues creating function within the MySQL forums, part of the Database Server Software category; --> I created two functions using MySQL Administrator. I copied the entire SQL code from the editor and pasted it ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 07:55 AM
Ted
 
Posts: n/a
Default issues creating function

I created two functions using MySQL Administrator. I copied the entire
SQL code from the editor and pasted it to a text editor. Then I told
MySQL Admijnistrator to execute the SQL.

The exact code is pasted below.

CREATE FUNCTION `alert_db`.`my_max_market_date` () RETURNS DATE
BEGIN
DECLARE mmmd DATE;
SELECT max(h_market_date) into mmmd FROM holdings_tmp;
RETURN mmmd;
END;

CREATE FUNCTION `my_market_date_less_a_week`() RETURNS DATE
BEGIN
DECLARE mmmd DATE;
DECLARE mmmdlaw DATE;
SELECT max(h_market_date) into mmmd FROM holdings_tmp;
SET mmmdlaw := DATE_SUB(mmmd,INTERVAL 7 DAY);
return mmmdlaw;
END;

We KNOW this code is precisely what MySQL Administrator showed in its
SQL editor.

I used the following statement to test whether or not this worked.

SELECT `my_max_market_date` (),`my_market_date_less_a_week` ();

It worked exactly as expected!

The problem is that when I try to put this into a script to be executed
from a command line such as the following, it fails!

mysql -u root -p --database=alert_db < Date_Calculation.sql

The error I get is:

ERROR 1064 (42000) at line 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 'DATE' at line 3

I do not understand why code that works fine within MySQL
Administrator's SQL editor should fail when submitting it through the
mysql client on the command line.

Yes, for our other development activities, it is enough that we have
these functions present and working. However, I have a suite of small
scripts that create the database de novo, and thus need to add code to
create these functions to my scripts.

Any ideas?

Thanks

Ted

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 07:55 AM
Axel Schwenke
 
Posts: n/a
Default Re: issues creating function

"Ted" <r.ted.byers@rogers.com> wrote:

> CREATE FUNCTION `alert_db`.`my_max_market_date` () RETURNS DATE
> BEGIN
> DECLARE mmmd DATE;
> SELECT max(h_market_date) into mmmd FROM holdings_tmp;
> RETURN mmmd;
> END;


> The problem is that when I try to put this into a script to be executed
> from a command line such as the following, it fails!
>
> mysql -u root -p --database=alert_db < Date_Calculation.sql
>
> The error I get is:
>
> ERROR 1064 (42000) at line 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 'DATE' at line 3


This is easy. The MySQL command line client *interprets* lines you
type. Especially it scans for the DELIMITER to break your lines into
SQL statements. The default delimiter is ';'. So your SQL statement
ends with the 'DATE' in line 3. It is thus incomplete and yields a
syntax error.

The solution is easy: specify a temporary new delimiter for mysql

mysql> DELIMITER //
mysql> CREATE FUNCTION my_max_market_date () RETURNS DATE
-> BEGIN
-> DECLARE mmmd DATE;
-> SELECT max(h_market_date) into mmmd FROM holdings_tmp;
-> RETURN mmmd;
-> END//
Query OK, 0 rows affected (1,95 sec)

mysql> DELIMITER ;

This trick is also shown in the manual:
http://dev.mysql.com/doc/refman/5.0/...procedure.html


XL
--
Axel Schwenke, Senior Software Developer, MySQL AB

Online User Manual: http://dev.mysql.com/doc/refman/5.0/en/
MySQL User Forums: http://forums.mysql.com/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 07:55 AM
Ted
 
Posts: n/a
Default Re: issues creating function


Thanks Axel

That was it.

Thanks

Ted

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:52 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com