简体   繁体   中英

mysql stored function syntax for dummies

I am appear to be having a bad case of brain flatulence at the moment. Can someone see the bleedingly obvious error in the following mysql stored function?

delimiter $$

drop function if exists test$$ 

create function test() 
returns boolean
begin
    return true;
end$$

I know it is dancing around the place, laughing its head off and waving its bare buttocks in my direction - but I just cant see it.

The original error was in a much larger hunk of code that I have cut down to the above - but I can't seem to remove much more without running out of code to remove. mysql version 5.7.14

Drop the function before setting the new delimiter

DROP function IF EXISTS `test`;

DELIMITER $$
USE `A_DB`$$
CREATE FUNCTION `test` ()
RETURNS BOOLEAN
BEGIN
    RETURN TRUE;
END$$

DELIMITER ;

For anyone who comes this way again the problem turned out not to be with the SQL. The issue was with the driver and my understanding of how mysql hangs together.

The delimiter statement is part of the MySQL client (command line) and is not part of any driver (or at least any on node i can find)

So if you are loading your sql by sucking in a string and squirting it at the database via a driver (nodejs mariasql in my case) then delimiter is not understood.

You either have to parse the delimiters out yourself - or be more elaborate and logon to the mysql client shell somehow and load the file. I will probably simply put a line such as %start sql stuff here %end and parse the sql into chunks - and squirt these at the driver.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM