Creating functions in mysql doesnt work - Error 1064 - mysql

I tried this example via phpMyAdmin
http://www.databasejournal.com/features/mysql/article.php/3569846/MySQL-Stored-Functions.htm
mysql> DELIMITER |
mysql>
CREATE FUNCTION WEIGHTED_AVERAGE (n1 INT, n2 INT, n3 INT, n4 INT)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE avg INT;
SET avg = (n1+n2+n3*2+n4*4)/8;
RETURN avg;
END|
This worked
DELIMITER |
The next statement gave:
Error
SQL query:
CREATE FUNCTION WEIGHTED_AVERAGE(
n1 INT,
n2 INT,
n3 INT,
n4 INT
) RETURNS INT DETERMINISTIC BEGIN DECLARE avg INT;
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 '' at line 5

As it is mentioned in the link:
As mentioned in the first stored procedures tutorial, we declare the "|" symbol as a delimiter, so that our function body can use ordinary ";" characters
You can write a lot of commands on different consecutive lines. But usually only when ';' is met, the hole statement is executed.
Putting a DELIMITER character means that MySQL should wait until this is closed no matter if you use ';' or not and only then to interpret what is between delimiters.

Related

Getting ERROR 1064 (42000) while declaring a variable in mysql 5.7

Im trying to declare a variable i mysql but strangely none of these commands are working. Ive snipped these commands from stackoverflow solved questions and while these seem to work for most of the people , its throwing same error on my system.
MySQL Server version: 5.7.26-0ubuntu0.18.10.1 (Ubuntu)
> declare #d1 decimal(10,2)
> declare d1 float(10);
> declare `d1` BIGINT(2);
> declare d1 INT;
All these are giving same error
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right syntax
to use
Also, this command print nothing for me, no error just blanks :-
-> DECLARE #COURSE_ID INT
-> SELECT #COURSE_ID = 5
-> PRINT #COURSE_ID
->
-> ;
->
->
User-defined variables can be accessed without declaring or initializing. Referring to a variable not been initialized will have a value of NULL and a type of string.
Using SET statement:
SET #X=1 ;
Could be accessed by the simple select statement:
SELECT #X; /*will result in 1*/
SET #X=#X+2; /*will update X to 3 now*/
SELECT #x; /*will result in 3*/
Being Session-specific, user variables can assign values from a limited set of data types: integer, decimal, floating-point, binary or nonbinary string, or NULL value.
For Local variables: They needs to be declared using DECLARE before
being accessed and can be used as local variables and the input
parameters inside a stored procedure
DELIMITER //
CREATE PROCEDURE Vnita(n int)
BEGIN
DECLARE X INT DEFAULT 1;
END
//
Don't forget to change delimiter back to semicolon...
DELIMITER ;
--Thanks for asking.

Use cursor within stored procedure [duplicate]

This question already has answers here:
syntax error for mysql declaration of variable
(2 answers)
Closed 4 years ago.
I'm trying to make a stored procedure that include a cursor inside it and fill one of my tables based on another table's data , every day .
I think I'm doing something wrong with syntax , I already wrote a simple Stored procedure with cursor and it worked totally right , but when it get a little more complicated it does not work any more .
I'm getting
Error Code: 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 'DECLARE brandId int ;' at line 1.
Please note that I'm using Mysql 5.7 and I'm creating this at phpmMyAdmin .
CREATE PROCEDURE ّFillCommentGrowth()
BEGIN
DECLARE brandId int;
DECLARE todayComment int ;
DECLARE brandCount int ;
DECLARE yesterdayComment int;
DECLARE crs CURSOR for SELECT id from brands;
SET brandCount = (SELECT count(*) from brands);
open crs;
WHILE brandCount > 0 DO
FETCH crs into brandId ;
set todayComment = (select IFNULL((select count(*) from comments as c where date(c.created_at) = date(subdate(NOW(),1)) and c.brand_id = brandId ),0));
set yesterdayComment = (select IFNULL((select commentAmount from commentsGrowth where moment = date(subdate(NOW(),2)) and brand_Ref= brandId),0));
INSERT INTO commentsGrowth
(
brand_Ref,
commentAmount,
diffrenceByYesterday,
degree,
AmountPercent,
moment)
VALUES
(brandId ,
todayComment,
(todayComment - yesterdayComment ) ,
(((ATAN(todayComment - yesterdayComment )*180))/PI()),
(degree*(1.1)),
date(subdate(NOW(),1)));
SET brandCount = brandCount - 1;
END WHILE;
close crs;
END
The error you are getting has nothing to do with cursor. You need to change the DELIMITER from standard semicolon (;). For example
DELIMITER //
CREATE PROCEDURE GetAllProducts()
BEGIN
SELECT * FROM products;
END //
DELIMITER ;
The DELIMITER statement changes the standard delimiter which is semicolon ( ; ) to another. In this case, the delimiter is changed from the semicolon( ; ) to double-slashes //. Why do we have to change the delimiter? Because we want to pass the stored procedure to the server as a whole rather than letting mysql tool interpret each statement at a time. Following the END keyword, we use the delimiter // to indicate the end of the stored procedure. The last command ( DELIMITER; ) changes the delimiter back to the semicolon (;).

MySQL function for login, return 1 or 0

i'm trying to make a login function on a website that is connected to a mySQL server. I have problems making the function work however.
CREATE FUNCTION login_function
(IN nick varchar(10),
IN pw varchar(10))
RETURNS NUMBER(1)
DETERMINIS
BEGIN
DECLARE v_result NUMBER(1);
SET v_result=0;
SELECT COUNT(username)
INTO v_result
FROM login
WHERE username=nick
AND password=pw;
RETURN v_result;
END;
##
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
'IN nick varchar(10),
IN pw varchar(10))
RETURNS NUMBER(1)
DETERMINIS
BEGIN'
at line 2.
What the database looks like:
https://gyazo.com/6ea3983e30bd564cd6f42ee58a327d5b
For functions you do not need to provide the direction of a parameter because all parameters must be in parameters. So, the definition of the function correctly:
CREATE FUNCTION login_function
(nick varchar(10),
pw varchar(10))
...
You also do not need the semicolon after the last end, and DETERMINIS should be DETERMINISTIC, and number(1) should be tinyint or bool.
You have several problems. First, you shouldn't include the IN keyword for parameters. Parameters are implied to be IN anyway.
Your next problem is that MySQL is reading your ; and thinking "I have reached the end of the statement". So it is trying to parse only part of your function before returning. You need to set your delimeter to something else so that it knows "process this entire block". Here's what happens when I try both in the console:
mysql> create function foo (IN i int, IN j int) returns int BEGIN
-> declare q int;
ERROR 1064 (42000): 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 'IN i int, IN j int) returns int BEGIN
declare q int' at line 1
mysql> create function foo (i int, j int) returns int BEGIN declare q int;
ERROR 1064 (42000): 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 1
mysql> create function foo (IN i int, IN j int) returns int begin return i*j; end //
ERROR 1064 (42000): 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 'IN i int, IN j int) returns int begin return j; end' at line 1
mysql> DELIMITER //
mysql> create function foo (i int, j int) returns int
-> BEGIN
-> declare q int;
-> SET q = i*j;
-> return q;
-> end;
-> //
Query OK, 0 rows affected (0.01 sec)
You also have a data type of NUMBER. This should be one of the integer varieties (tinyint, or int(1), for example) or boolean.
Finally, you've inserted some sort of keyword DETERMINIS into your function creation. I've not seen that in any MySQL documentation before. (As shadow pointed out, it should be DETERMINISTIC… I admit that I missed that).

What is wrong this simple function?

Here is a simple function I am creating for purpose of practice. But receive the given error.
DELIMITER $$
CREATE FUNCTION Weighted_Average(n1 INT, n2 INT, n3 INT, n4 INT)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE avg INT;
SET avg = (n1 + n2 + n3*2 + n4*4)/8;
RETURN avg;
END;
DELIMITER $$
Error Code: 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 'n4*4)/8;   RETURN avg;  END' at line 6
I am comparing it to this post:
MySQL CREATE FUNCTION Syntax
Someone with experience can probably point out my mistake while I am getting no where. I don't see what is wrong where the error is asking me to look. Note: I am using workbench.
Are you sure you don't mean the following, mainly the bottom of it?
DROP FUNCTION IF EXISTS Weighted_Average;
DELIMITER $$
CREATE FUNCTION Weighted_Average(n1 INT, n2 INT, n3 INT, n4 INT)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE avg INT;
SET avg = (n1 + n2 + n3*2 + n4*4)/8;
RETURN avg;
END;$$
DELIMITER ;
A DELIMITER is necessary for some client-side environments to delineate the beginning and end of blocks, and to change the end-of-lines for statements.
As the DELIMITER is defaulted to ; out of the box, we know how to end a sql line with it, and never think about it. Until ...
When it comes to specialty blocks like CREATE PROCEDURE , FUNCTION, EVENT, TRIGGER, there needs to be some protocol for the client and server to know where the whole thing ends.
So, for clients like MySQL Workbench and similar, we use DELIMITER blocks. We change it to something funny up top, code as usual, and do the ending as seen above. Setting the DELIMITER back to our normal ;
They are not needed for PHPMyAdmin. And presumably not so for SqlFiddle

mysql 5.1: how can i use benchmark() command to test a call to a stored procedure?

I'm trying to benchmark a stored procedure.
select benchmark(100000000,(select 1));
this benchmark works
but the following benchmark doesn't:
do benchmark(1000,(call test_login_user('a')));
it produces the following error:
ERROR 1064 (42000): 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 'call xpofb_login_user('a')))' at line 1
any ideas how to resolve the issue ?
You can't do this with benchmark(), but you could create a stored procedure to do it.
Here's an example:
delimiter $$
create procedure benchmark_test_login_user (p_username varchar(100),
p_count int unsigned)
begin
declare v_iter int unsigned;
set v_iter = 0;
while v_iter < p_count
do
call test_login_user(p_username);
set v_iter = v_iter + 1;
end while;
end $$
delimiter ;
call benchmark_test_login_user('a',1000);
You can't
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_benchmark
Only scalar expressions can be used. Although the expression can be a subquery, it must return a single column and at most a single row. For example, BENCHMARK(10, (SELECT * FROM t)) will fail if the table t has more than one column or more than one row.