3

Here is my code:

create function getten() returns integer
begin
return 10;
end

giving me error message:

1064 - 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 3 

I have no idea what the '' is all about. Any help please?

0

5 Answers 5

4

First of all, SQL code is written in big letters. Second, please remember indention.

DELIMITER $$
CREATE FUNCTION getten()
  RETURNS INTEGER
  LANGUAGE SQL
BEGIN
  RETURN 10;
END;
$$
DELIMITER ;

should work like this.

Sign up to request clarification or add additional context in comments.

Comments

2

This works for me...

DELIMITER $$
 CREATE FUNCTION getten()
  RETURNS INTEGER
  BEGIN
  RETURN 10;
  END;
 $$

DELIMITER ;

SELECT getten();
+----------+
| getten() |
+----------+
|       10 |
+----------+

Comments

0

Try this you need to define the type of return in function

DELIMITER $$

CREATE

    FUNCTION `getten`()
    RETURNS INTEGER

    BEGIN
RETURN 10;
    END$$

DELIMITER ;

And after creation you can use it like

SELECT `getten`()

Comments

0

When you have only one statement to be executed, you need not use begin - end.

create function getten() returns integer return 10;

Otherwise you need default syntax to bind multiple statements within begin - end block.

And to restrict the database engine interpret the statements immediately when it finds a default statement close instruction by a semi colon, we use custom delimiter.

delimiter //

create function getten() returns integer
begin 
  return 10; 
end;
//

delimiter ;

It would be a good practice to follow using the block type body rather than using an inline single statement.

1 Comment

You should always use them, just for clarity in my opinion. But i guess, that is user preference:)
0

All your answers were great, but only worked after I added 'deterministic' before 'begin'.

Thanks a lot.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.