Syntax error of ''? - mysql

I can't seem to figure out where this is coming from... MySQL give me a syntax error of an empty quote and gives me a line number that doesn't seem to be wrong at all. Worse still, deleting the loop the line number points to still gives me the same error, just with a different line number.
ERROR 1064 (42000) at line 13: 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 39
Talk about unhelpful feedback from MySQL!
The code in question is a stored function, and I ran into this when trying to apply the answer to another question. The updated code is available here.
EDIT: #MarkByers, here's the function reduced as low as I could get it while still triggering the error:
DROP FUNCTION IF EXISTS months_within_range;
DELIMITER //
CREATE FUNCTION months_within_range(starts_at DATE, ends_at DATE, filter_range VARCHAR(255)) RETURNS TINYINT
BEGIN
SET #matches = 1;
IF #matches >= 1 THEN RETURN 1;
ELSE RETURN 0;
END//
DELIMITER ;

You are missing the END IF
IF #matches >= 1 THEN RETURN 1;
ELSE RETURN 0;
END IF;
And the reason RETURN IF(#matches >= 1, 1, 0); works is because this is the IF function, which is different from the IF statement

Related

Returning Error in a Dynamic MySql Procedure

I am developing some functions/procedures for a much larger system, that is gona be used in others tables, so i tried to create a Dynamic MySql Procedure, to pass the name of the tables by variables,and get an "InOut" variable to return the result that i want, but it is returning me some Syntax error, i don't know where i am geting wrong in the code, so if someone knows a way better to do it, or get where the error is, i will be very thankfull!
Here is The Error that is returning:
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 'If Exists (Select cliente26 From da26s9 Where da26s9.c' at line 1.
Here is how i am Calling the Procedure:
Call FilTabMes('da26s9','codigo26','cliente26','002715',#string);
Here is The Procedure that i am struggling with:
CREATE PROCEDURE `FilTabMes`(vTabMes_ Char(32),vCodMes_ Char(32),vCodTab_ Char(32),vComFin_ Integer,InOut vResPes_ Char(32))
Begin
Declare vSelTab VarChar(1000);
Set #vSelTab = CONCAT('Select ',vCodTab_,' From ',vTabMes_,'
Where ',vTabMes_,'.',vCodMes_," Like ",vComFin_);
Set #vSelTab = CONCAT('If Exists(',#vSelTab,')
Begin
Set vResPes_ = "T";
End
Else
Begin
Set vResPes_ = "F";
End
End If');
Prepare stmt1 From #vSelTab;
Execute stmt1;
End

Loop syntax error in MySQL cursor

I have been trying to use a cursor and following a tutorial I found online, I was able to come up with the following cursor.
DELIMITER $$
CREATE PROCEDURE customers_with_oldest_version (INOUT customerCount varchar(4000))
BEGIN
DEClARE customers_with_oldest_version CURSOR FOR
select * from CustomerSoftware where software in (select min(minimumSoftware) from ProductSoftware);
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET #finished = 1;
set #row_entry = "";
open customers_with_oldest_version;
get_customers: LOOP
FETCH customers_with_oldest_version INTO #row_entry;
IF #finished = 1 THEN
LEAVE get_customers;
END IF;
SET #customerCount = #customerCount + 1;
END LOOP;
CLOSE customers_with_oldest_version;
END$$
DELIMITER ;
But I am unable to create this procedure, since phpmyadmin is giving me an error saying that
#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 '#row_entry; IF #finished = 1 THEN LEAVE get_customers; END IF; SET #c' at line 16
What am I missing here?
You can't fetch into a user-defined variable. This is a bug that has been around for many years, see https://bugs.mysql.com/bug.php?id=2261
Declare a regular local variable for it.

Getting Error While Defining Stored Procedure MySQL

I am trying to define stored procedure in mysql but getting error i am unable to understand what i am doing wrong.
CREATE PROCEDURE distance(lat1 Float,long1 Float,lat2 Float,long2 Float,OUT distance Float)
BEGIN
SET distance = ROUND((ACOS(SIN(RADIANS(lat1))*SIN(RADIANS(lat2))+COS(RADIANS(lat1))*COS(RADIANS(lat2))*COS(RADIANS(lon2-lon1)))*6371), 2);
END
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 '' at line 4
Use Delimiters to resolve this:
delimiter //
CREATE PROCEDURE distance(lat1 Float,long1 Float,lat2 Float,long2 Float,OUT distance Float)
BEGIN
SET distance = ROUND((ACOS(SIN(RADIANS(lat1))*SIN(RADIANS(lat2))+COS(RADIANS(lat1))*COS(RADIANS(lat2))*COS(RADIANS(lon2-lon1)))*6371), 2);
END //
delimiter ;

Sql Syntax giving unknown syntax error

I am having a problem with my mysql statement.
basically the code works perfectly when inserting, but as soon as the addnew variable=false and it switches to update it gives me an error that i cannot solve.
The Code:
procedure Tadddomain.BitBtn2Click(Sender: TObject);
Var
PrevSql:String;
ID:String;
begin
With Datalive.domains Do
Begin
id:=fieldbyname('id').AsString;
Active:=False;
prevsql:=sql.Text;
Sql.Clear;
Params.Clear;
Addparam(Datalive.domains,'client_id',ftinteger,datalive.clients.FieldByName('id').AsString);
Addparam(Datalive.domains,'domain_name',ftString,Edit1.Text);
Addparam(Datalive.domains,'register_date',ftdate,DateTimePicker1.Date);
Addparam(Datalive.domains,'registered_until',ftdate,DateTimePicker2.Date);
if addnew=true then
Sql.Text:='Insert into domains (client_id,domain_name,register_date,registered_until) VALUES (:client_id,:domain_name,:register_date,:registered_until)'
Else if addnew=False then
Sql.Text:='Update domains (domain_name=:domain_name, register_date=:register_date, registered_until=:registered_until) where id='''+id+'''';
Showmessage(sql.text);
execsql;
sleep(100);
sql.Text:=prevsql;
active:=True;
done:=True;
adddomain.Close;
End;
end;
The Error:
Project project1.exe raised exception class EZSQLException with message 'SQL 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 '(domain_name='asd',register_date='2014-11-09',registered_until='2015-11-09') w' at line 1'.
Any Assistance would be great, I have searched and searched and cannot find the fault.
Update:
I changed the edit code as suggested below, and now no errors apear what so ever. But also nothing happens. It does not edit the record.
if addnew=true then
Sql.Text:='Insert into domains (client_id,domain_name,register_date,registered_until) VALUES (:client_id,:domain_name,:register_date,:registered_until)'
Else if addnew=False then
Begin
sql.Add('Update domains');
sql.Add('set domain_name=:domain_name,');
sql.Add('register_date=:register_date,');
sql.Add('registered_until=:registered_until');
sql.Add('where id=:id');
End;
Update syntax is like this
Update domains
set domain_name = :domain_name,
register_date = :register_date,
registered_until = :registered_until
where id = :id
You declare the "client_id" parameter not the "id" parameter you use in the update statement.
where id = :id
should be
where id = :client_id
I have found the problem.
If i define the parameters before i use the sql.add() function the parameters are discarded.
When I moved the
Addparam(Datalive.domains,'client_id',ftinteger,datalive.clients.FieldByName('id').AsString);
Addparam(Datalive.domains,'domain_name',ftString,Edit1.Text);
Addparam(Datalive.domains,'register_date',ftdate,DateTimePicker1.Date);
Addparam(Datalive.domains,'registered_until',ftdate,DateTimePicker2.Date);
to just before the execsql; the problem was solved.

MySQL problem using CREATE FUNCTION in cPanel

I have been receiving always an error while creating mysql function in cpanel with below code.
But the below code is running properly while creating mysql function in SQL Manger 2007.
Anyone tell me what is wrong with this code for cpanel.
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 12
(0.81 sec)
CREATE FUNCTION F_test (topsbwynum INTEGER)
RETURNS float(20,2)
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
BEGIN
DECLARE FinalPrice VARCHAR(20);
DECLARE err VARCHAR(20);
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET err = 1;
IF(sbwynum > 0)THEN
IF(FinalPrice > 0)THEN
RETURN FinalPrice;
ELSEIF(adjprice > 0)THEN
RETURN adjprice;
ELSEIF(price > 0)THEN
RETURN price;
ELSEIF(estprice > 0)THEN
RETURN estprice;
ELSE
RETURN 0;
END IF;
ELSE
RETURN 0;
END IF;
END;
Thanks a lot.
For multi-line statements, you can't use the same character (";", by default) to delimit the individual statements within the function definition and the entire CREATE statement. If you try, the creation statement ends at the first ";" (in the sample code, it's after the first DECLARE). Since the statement syntax error is at the end of the truncated statement, the error message gives '' as the part of the statement where the error occurs.
From the command line client, you'd use the delimiter command to change the delimiter used by the client to (e.g.) ";;", then end the creation statement with this delimiter:
delimiter ;;
CREATE FUNCTION F_test (topsbwynum INTEGER)
...
END;;
delimiter ;
In phpMyAdmin, there's a "delimiter" field that fulfills the same purpose.