MySQL error #1064 on create function - mysql

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?

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.

This works for me...
DELIMITER $$
CREATE FUNCTION getten()
RETURNS INTEGER
BEGIN
RETURN 10;
END;
$$
DELIMITER ;
SELECT getten();
+----------+
| getten() |
+----------+
| 10 |
+----------+

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`()

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.

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

Related

Wrong MYSQL Create Function syntax?

I'm trying to create a function like this in MYSQL:
DELIMITER $$
CREATE FUNCTION `submit`(title VARCHAR(45)) RETURNS INT
BEGIN
DECLARE articleId INT;
INSERT INTO Articles (`Title`) VALUES (title);
SET #articleId = LAST_INSERT_ID();
RETURN (articleId);
END
$$ DELIMITER ;
No matter how I change it (including removing everything in the body and placing a "return 1;" instead) I get:
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 'DELIMITER $$ CREATE FUNCTION submit(title v' at line 1
What am I missing?
I'm using MySQL 5.5
EDIT
Apparently, MySQL is ignoring the first statement (DELIMITER $$), failing on the first ; it finds, right after articleId INT in the 4th line. I had to reduce the code to the shortest form possible to make sure that's the case.
So I guess my question now is - Why is the DELIMITER keyword ignored?

You have an error in your SQL syntax when creating a procedure. MYSQL

I get that error when I try to create a procedure. I dont know what's failing and I searched a lot if someone has the same error than me, but usually they mistake at delimiters, and I think i have them right.
"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 SUM(robatori.quantitat_robada) FROM robatori WHERE param1=jugador_lla' at line 1 "
My query is the next:
DELIMITER //
CREATE PROCEDURE robatoris (IN param1 VARCHAR)
SELECT SUM(robatori.quantitat_robada) FROM robatori WHERE param1=jugador_lladre;
//
DELIMITER ;
Thanks you all, that's my first question here. :)
You are missing the length for input param on type VARCHAR.
Change it like the following:
DELIMITER //
CREATE PROCEDURE robatoris ( IN param1 VARCHAR(255) )
SELECT SUM(robatori.quantitat_robada)
FROM robatori
WHERE param1=jugador_lladre;
//
DELIMITER ;
As you have only statement to execute, the BEGIN - END block was optional.
But it is advised to practice it in all cases.
DELIMITER //
CREATE PROCEDURE robatoris ( IN param1 VARCHAR(255) )
BEGIN
SELECT SUM(robatori.quantitat_robada)
FROM robatori
WHERE param1=jugador_lladre;
END;
//
DELIMITER ;
Add BEGIN and END. Use TEXT type:
DELIMITER //
CREATE PROCEDURE robatoris (IN param1 TEXT)
BEGIN
SELECT SUM(robatori.quantitat_robada)
FROM robatori
WHERE param1=jugador_lladre;
END//
DELIMITER ;

error in writing procedure in Phpmyadmin

I'm trying to write a stored procedure in Mysql phpmyadmin, the procedure is
DELIMITER $$
DROP FUNCTION IF EXISTS `shopping_portal`.`f_authenticate_admin`$$
CREATE PROCEDURE =`root`#`localhost` FUNCTION `f_authenticate_admin`(l_username VARCHAR(50),l_password VARCHAR(50)) RETURNS int(11)
BEGIN
DECLARE exist INT DEFAULT 0;
SELECT count(*) INTO exist FROM admin WHERE username=l_username and password=MD5(l_password);
RETURN exist;
END$$
DELIMITER ;
but it is throwing the error
#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 '=`root`#`localhost` FUNCTION `f_authenticate_admin`(l_username VARCHAR(50),l_pas' at line 1
Help me in writing this procedure. Thanks in advance.
Well, multiple issues:
You're mixing procedures and functions, those are two different stories. And you're probably looking for DEFINER with your = root#localhost. And you want to use single quotes instead of backticks (I'm not sure though, if that's really an issue). Anyway, let me rewrite it for you...
DROP PROCEDURE IF EXISTS `shopping_portal`.`f_authenticate_admin`;
DELIMITER $$
CREATE DEFINER = 'root'#'localhost' PROCEDURE `f_authenticate_admin`(IN l_username VARCHAR(50), IN l_password VARCHAR(50), OUT result tinyint)
BEGIN
SELECT EXISTS(SELECT 1 FROM admin WHERE username=l_username and password=MD5(l_password)) INTO result;
END$$
DELIMITER ;
You then call it like this:
CALL f_authenticate_admin('test_username', 'a_password', #a_variable);
Then you have your result in #a_variable.
SELECT #a_variable;
Result is either 1 or 0.

mysql error, stored procedure creation

I am new to mysql and I cant see why I have an error when I create my stored procedure.
DELIMITER |
CREATE PROCEDURE lastscan(IN task_id_var INT)
BEGIN
SELECT COUNT(*) FROM debugger WHERE task_id=task_id_var INTO #total|
SET #total=#total+1|
INSERT INTO debugger SET scan_num=#total, task_id=task_id_var|
END|
DELIMITER;
I get :
#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 also dont get, why do I need to use that delimiter syntax.. ? DELIMITER | and then again DELIMITER;...what its function
DELIMITER |
CREATE PROCEDURE lastscan(IN task_id_var INT, IN file_name_var VARCHAR(110))
BEGIN
SELECT COUNT(*) FROM debugger WHERE task_id=task_id_var INTO #total;
SET #total=#total+1;
INSERT INTO debugger SET scan_num=#total, task_id=task_id_var, file_name=file_name_var;
END|
DELIMITER;
This works for me. no need to put | delimiter in sored procedure. I think it is meant to be for the stored procedure and not for what is inside the body
You can't simply assign variables like that, you need the SET keyword first.
http://dev.mysql.com/doc/refman/5.1/en/set-statement.html
So you code should be something like this (tested with phpMyAdmin):
DELIMITER //
CREATE PROCEDURE lastscan(IN task_id_var INT)
BEGIN
SELECT COUNT(*) FROM debugger WHERE task_id=task_id_var INTO #total;
SET #total=#total+1;
INSERT INTO debugger SET scan_num=#total, task_id=task_id_var;
END;//
DELIMITER ;
The DELIMITER keyword is used to stop additional semicolons in your procedure to be the end of the current statement, so by redefining the delimiter to // MySql will process the whole CREATE PROCEDURE-block as one single statement and not stop at the first semicolon but instead wait for the first occurrence of //.
http://dev.mysql.com/doc/refman/5.1/en/stored-programs-defining.html

mysql delimiter error

Modifed.
DROP FUNCTION IF EXISTS PersonName;
DELIMITER |;
CREATE FUNCTION PersonName( personID SMALLINT )
RETURNS CHAR(20)
BEGIN
DECLARE pname CHAR(20) DEFAULT '';
SELECT name INTO pname FROM family WHERE ID=personID;
RETURN pname;
END;
|
DELIMITER ;
whats wrong with this code? i get following error with it.
There seems to be an error in your SQL query. The MySQL server error
output below, if there is any, may
also help you in diagnosing the
problem
ERROR: Unknown Punctuation String #
102 STR: |; SQL: DROP FUNCTION IF
EXISTS PersonName;# MySQL returned an
empty result set (i.e. zero rows).
DELIMITER |; DELIMITER |; DELIMITER |;
DELIMITER |; DELIMITER |; DELIMITER |;
DELIMITER |;
SQL query:
DELIMITER |;
MySQL said: Documentation
#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 'DELIMITER |' at line 1
I would remove the semicolon after END.
...
END
|
DELIMITER ;
Re your comment, you can't use the current delimiter when declaring a new delimiter. That sounds confusing, but consider if you do this:
DELIMITER |;
Now MySQL would think the delimiter is "|;" (two characters, a pipe and a semicolon). If you think about it, DELIMITER must be treated in a special way by the MySQL client. It's the only statement that can't be followed by the current delimiter.
So when setting the delimiter to pipe, do this:
DELIMITER |
When setting it back to semicolon, do this:
DELIMITER ;
FWIW, I ran the following with no error on my local test database on MySQL 5.0.75:
DROP FUNCTION IF EXISTS PersonName;
DELIMITER |
CREATE FUNCTION PersonName( personID SMALLINT )
RETURNS CHAR(20)
BEGIN
DECLARE pname CHAR(20) DEFAULT '';
SELECT name INTO pname FROM family WHERE ID=personID;
RETURN pname;
END
|
DELIMITER ;
Try this:
DROP FUNCTION IF EXISTS PersonName;
DELIMITER |
CREATE FUNCTION PersonName( personID SMALLINT )
RETURNS CHAR(20)
BEGIN
DECLARE pname CHAR(20) DEFAULT '';
SELECT name INTO pname FROM family WHERE ID=personID;
RETURN pname;
END;
|
DELIMITER ; /* <-- add a space between DELIMITER and the semicolon */
Try this if you are using phpMyAdmin:
http://dotnetfish.blogspot.com/2009/07/1064-you-have-error-in-your-sql-syntax.html
I was have problems with the delimiter. I was using Navicat, then I rolled over to MySql workbench and the problem is solved. Workbench inserts the delimiter into code...
In your last line where you're restoring the delimiter to semicolon you need a space between DELIMITER and ; i.e.
DELIMITER ;
Best solution is, which I tried after getting the above error. The code should be like
Delimiter //
Create function or procedure
Write your function or procedure here...
End (without semicolon)
//
Delimiter ; (semicolon with space)
I recently stumbled upon integrating MySQL Stored Procedure with Liquibase Scripts using Spring Boot Project.
Paste your MySQL Stored Procedure in GetCustomersWithCreditCardExpiry.sql File which is saved under liquibase directory.
DROP PROCEDURE IF EXISTS GetCustomersWithCreditCardExpiry;
#
CREATE PROCEDURE GetCustomersWithCreditCardExpiry(IN customer_status VARCHAR(10), IN time_period INT(11))
BEGIN
SELECT cs.*
FROM customer cs
JOIN credit_card cc
ON cc.id = cs.credit_card_id
WHERE cs.status = customer_status
AND cc.expiry_date < DATE_ADD(now(), INTERVAL time_period HOUR);
END
#
Add the following changeset in Liquibase Script with DELIMITER as #
<changeSet id="1" author="ishaq" runOnChange="true">
<sqlFile path="procedures/GetCustomersWithCreditCardExpiry.sql"
relativeToChangelogFile="true"
endDelimiter="#"
splitStatements="true"/>
</changeSet>
Consuming the Stored Procedure within the Spring Boot Java Application.
#Query(value = "CALL GetCustomersWithCreditCardExpiry(:customer_status, :time_period);", nativeQuery = true)
List<CustomerCreditCardRenewal> GetCustomersWithCreditCardExpiry(#Param("customer_status") String customer_status,
#Param("time_period") Integer time_period);
Entity Class to map the results
#Builder(toBuilder = true)
#AllArgsConstructor
#NoArgsConstructor
#Getter
#Data
#Entity
#NamedStoredProcedureQuery(name = "CustomerCreditCardRenewal.getCustomersWithCreditCardExpiry",
procedureName = "GetCustomersWithCreditCardExpiry",
parameters = {
#StoredProcedureParameter(
mode = ParameterMode.IN,
name = "customer_status",
type = String.class
),
#StoredProcedureParameter(
mode = ParameterMode.IN,
name = "time_period",
type = Integer.class
)
})
public class CustomerCreditCardRenewal {
//... members
}
When the spring boot application runs the changeset in Liquibase script will be executed without any errors.
By invoking the Repository Method using a Service Class the required results from the Stored Procedure will be populated.
Hope this will help someone.
You have to add delimiter $$ in the beginning and in the last of the mysql script you should to end by the delimiter
I'm going to throw this in the mix because it may help other people with this issue.
If you are using phpMyAdmin, below the SQL entry box there is a delimiter box where you can type the delimiter to use for the query. You can put the outside delimiter here if you want to and then you don't need the delimiter directives.
For example, if you put the delimiter $$ inside the box you could use the following format for your query.
CREATE FUNCTION
blah blah...
END
$$
Some people may find this easier.
Running this Dbeaver gave this error... run this in mysql shell and you will not get any error..