Sql Syntax giving unknown syntax error - mysql

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.

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

MySQL Error pls-00103

I am making a MySQLand getting an error I dont know how to fix it. What the function should be doing is asking the user for input, this input should be stored in the corresponding variables and return a variable. But when i try run the function I am getting errors on the lines that store the variables. I am quite new to mySQL so i might be doing something stupid. Any help will be great thank you
This is is the mysql function
create or replace FUNCTION AD_AGENCY_INFO(agency_id in agency_id%type)
RETURN agency_id
DECLARE
v_no_of_runs AD_AGENCY.NO_OF_AD_RUNS%TYPE = '&enter_number_of_Runs';
v_credit_worthy AD_AGENCY.CREDIT_WORTHY%TYPE = '&enter_credit_worthy';
v_available_slots AD_AGENCY.AVAILABLE_SLOTS%TYPE = '&enter_available_slots';
v_status AD_AGENCY.STATUS%TYPE = '&enter_status';
BEGIN
insert into ad_agency values (agency_id, v_no_of_runs, v_credit_worthy,v_available_slots, v_status);
insert into ad (agency_id) values (agency_id);
commit;
RETURN (agency_id));
END AD_AGENCY_INFO;
The error that i am getting is the following and is the same for lines 7,8,9
Error(6,45): PLS-00103: Encountered the symbol "=" when expecting one of the following: := ( ; not null range default character The symbol ":= was inserted before "=" to continue.
Try changing your declare statement(s) to be
v_no_of_runs AD_AGENCY.NO_OF_AD_RUNS%TYPE := '&enter_number_of_Runs'

Error on Creating Stored Function in MySQL 5.0.5

I'm usually working with mssql or postgres, but I need to use MySql in the current project.
Now I have a problem with creating a stored function in MySQL 5.0.5.
I'm getting the following 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
My Code looks like that:
CREATE FUNCTION aip_sf_choice_valid (request_id INT)
RETURNS INT
BEGIN
DECLARE sf_result INT;
SET sf_result = 1;
RETURN sf_result
END
I'm really out of ideas. I'm glad for any help!
You need to change the default delimiter from ';' within the SP body. More info on that here: http://dev.mysql.com/doc/refman/5.1/en/create-procedure.html
Try this:
DELIMITER $$
CREATE FUNCTION aip_sf_choice_valid (request_id INT)
RETURNS INT
BEGIN
DECLARE sf_result INT;
SET sf_result = 1;
RETURN sf_result;
END
Should it have a semicolon after "END"?

Syntax error of ''?

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

MySQL 5.1.39 SQL syntax error

I have made an upgrade for my MySQL Server to 5.1.39 and now when I run SQL scripts (which had worked previously) - it throws error. I have checked syntax many times and I couldn't find any incompatible code parts. Please suggest any solution for this problem.
Error message
Mysql::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 'CREATE FUNCTION clean_dymmy_table (dummy_name VARCHAR(255)) RETURNS V' at line 3:
SQL code:
/*DELIMITER //*/
DROP FUNCTION IF EXISTS clean_dymmy_table;
CREATE FUNCTION clean_dymmy_table (dummy_name VARCHAR(255)) RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
DECLARE temp_val VARCHAR(255);
SET temp_val = dummy_name;
-- Test
SET temp_val = REPLACE(temp_val, 'Tmp ', '');
SET temp_val = REPLACE(temp_val, ' TmP', '');
SET temp_val = REPLACE(temp_val, 'TMP ', '');
SET temp_val = REPLACE(temp_val, ' TMP', '');
SET temp_val = REPLACE(temp_val, ' tmp', '');
RETURN dummy_name;
END/*//*/
Not sure why you removed the DELIMITER part, but when I add that back in, it runs fine:
DELIMITER // -- you have to change what MySQL expects between commands
DROP FUNCTION IF EXISTS clean_dymmy_table // -- tell it a new command's coming
CREATE FUNCTION clean_dymmy_table (dummy_name VARCHAR(255))
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
-- now this can be parsed as part of the current command.
DECLARE temp_val VARCHAR(255);
SET temp_val = dummy_name;
-- Test
RETURN dummy_name;
END
// -- Now you're done with that command.
-- go back to semi-colons, because otherwise life is too zany for me.
DELIMITER ;
(This was in 5.1.54... but I don't think that should matter)
A hunch: There seems to be a MySQL Bug in the 5.1.x line pertaining to DELIMITER that may be biting you here:
#46429 use DELIMITER command in MySql.Data.MySqlClient.MySqlScript
Though that heavily depends on how you're calling it, given cwallenpoole's response, your symptom does suggest it may well be that bug, given your MySQL version. Is upgrading a possibility for you?