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
Related
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?
I'm creating procedure which is having two parameters , one is p_cursor of type SYS_REFCURSOR (OUT param) and the other one is p_rank of type INT(IN param). But it showing an error.
DELIMITER $$
CREATE PROCEDURE sp_student(p_cursor OUT SYS_REFCURSOR,p_rank IN INT)
BEGIN
OPEN p_cursor FOR SELECT * FROM student WHERE rank = p_rank;
END$$
DELIMITER ;
the error what I'm getting is,
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 'OUT SYS_REFCURSOR,p_rank IN INT)
BEGIN
OPEN p_cursor FOR SELECT * FROM st' at line 1
I think I'm syntactically wrong for SYS_REFCURSOR.. please check my code and let me realise my mistake.
thanks in advance
mysql doesnt have refcursor like oracle, if u r planning to write a stored procedure that returns multiple rows/result set in mysql just do
DROP procedure IF EXISTS `sample`;
DELIMITER $$
CREATE PROCEDURE `sample`(p_rank IN INT)
BEGIN
select * from MyTable where id=p_rank;
END$$
DELIMITER ;
call sample();
this will return a result set. which u can use.
I'm following some tutorials on mysql function creation but I keep getting the following error.
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:
CREATE FUNCTION getstatisticscount (h VARCHAR(35),d date)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE tel INT;
SELECT count(hash) into tel from statsitieken where hash=h and lastvisit between concat(d,' 00:00:00') and concat(d,' 23:59:59') group by hash;
RETURN tel;
END;
I can for my life not find where line 5 is, but no matter which line I put it on, I keep getting this error.
If I remove this function from the sql fiddle code it's all fine.
I can't find what's wrong with it... except maybe flawed tutorials.
http://sqlfiddle.com/#!2/70f0a
Use Delimiter
delimiter //
CREATE FUNCTION getstatistics(h VARCHAR(35),d date)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE tel INT;
SELECT count(hash) INTO tel
FROM statistics
WHERE
hash=h
AND lastvisit BETWEEN concat(d,' 00:00:00') AND concat(d,' 23:59:59')
GROUP BY hash;
RETURN tel;
END
//
DELIMITER ;
For more info: http://dev.mysql.com/doc/refman/5.1/en/stored-programs-defining.html
Edit: basically since your stored procedure separates out statements with semicolons (;) and the method that defines your procedure ALSO uses semicolons to separate out statements, it's hard to impossible for MySQL to figure out where your procedure begins and ends.
I also edited the SQL statement above to return the delimiter back to the default semicolon.
I have a simple stored procedure which inserts records into four character fields in table. Below is the procedure
CREATE PROCEDURE dowhile()
BEGIN
DECLARE I INT DEFAULT 5
v1loop: WHILE I < 10000 DO
INSERT INTO TestTable1(A,B,C,D)
SELECT CONCAT(I,'A'), CONCAT(I,'B'), CONCAT(I,'C'), CONCAT(I,'D')
SET I = I + 1
END WHILE v1loop
END;
Checked online - there are no free MSSQL to MYSQL SQL Conversion Tools
Error is
- SQL Syntax Error in Insert - SELECT Statement
I have checked the syntax this seem to be correct.
Any pointers for this would be helpful.
Not to bad actually, you just need to add some semi-colons and change MySQL's default delimiter. This needs to be done since we're using SQL inside SQL.
DELIMITER $$
CREATE PROCEDURE dowhile()
BEGIN
DECLARE I INT DEFAULT 5;
v1loop: WHILE I < 10000 DO
INSERT INTO TestTable1(A,B,C,D)
SELECT CONCAT(I,'A'), CONCAT(I,'B'), CONCAT(I,'C'), CONCAT(I,'D');
SET I = I + 1;
END WHILE v1loop;
END$$
DELIMITER ;
Just tested this on my MySQL server.
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.