I am getting the following error in my code:
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 'END' at line 20
CREATE PROCEDURE updateUser(
IN firstname VARCHAR(20),
IN Vlastname VARCHAR(20),
IN Vemail VARCHAR(50),
IN Vpassword VARCHAR(100),
IN Vyear INT(5),
IN Vgender VARCHAR(10),
IN Vprefer VARCHAR(200),
IN Vinterested VARCHAR(200),
IN Vabout VARCHAR(200),
IN Vid INT(11)
)
BEGIN
UPDATE users SET
name = (CASE WHEN firstname IS NOT NULL THEN firstname ELSE name
END),
lastname = (CASE WHEN vlastname IS NOT NULL THEN vlastname ELSE lastname
END)
WHERE id = Vid
END;
When developing stored procedures in MySQL, you must set a delimiter different from ; to properly end the stored program statement. This allows the use of ; to delineate multiple statements within the procedure.
Per MySQL 25.1 Defining Stored Programs docs:
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.
Therefore consider below adjustment:
DELIMITER // -- TEMPORARILY CHANGE DELIMITER FROM ;
CREATE PROCEDURE updateUser (
IN firstname VARCHAR(20),
IN Vlastname VARCHAR(20),
IN Vemail VARCHAR(50),
IN Vpassword VARCHAR(100),
IN Vyear INT(5),
IN Vgender VARCHAR(10),
IN Vprefer VARCHAR(200),
IN Vinterested VARCHAR(200),
IN Vabout VARCHAR(200),
IN Vid INT(11)
)
BEGIN
UPDATE `users`
SET
`name` = CASE WHEN `firstname` IS NOT NULL THEN `firstname` ELSE `name` END,
`lastname` = CASE WHEN `vlastname` IS NOT NULL THEN `vlastname` ELSE `lastname` END
WHERE `id` = Vid; -- SEMICOLON HERE TO SEPARATE STATEMENTS
END
// -- PROPERLY END PROCEDURE STATEMENT
DELIMITER ; -- RESET DELIMITER BACK TO ;
you are missing a semicolon after The WHERE clause
And you can simplify it
DELIMITER //
CREATE PROCEDURE updateUser(
IN firstname VARCHAR(20),
IN Vlastname VARCHAR(20),
IN Vemail VARCHAR(50),
IN Vpassword VARCHAR(100),
IN Vyear INT(5),
IN Vgender VARCHAR(10),
IN Vprefer VARCHAR(200),
IN Vinterested VARCHAR(200),
IN Vabout VARCHAR(200),
IN Vid INT(11)
)
BEGIN
UPDATE users SET
name = COALESCE (firstname, name),
lastname = COALESCE (vlastname,lastname)
WHERE id = Vid;
END//
DELIMITER ;
Related
create PROCEDURE PR_Credit_Total(
in newidpiece int,
in newnpiece bigint,
in newnop varchar(60),
in newdateengagement date,
in newdatefacture date,
in newlocalité varchar(50),
in newtournee int,
in newnpolice bigint,
in newservice varchar(20),
in newmontant float,
in newecheance varchar(7),
in newcreated varchar(25),
in eventvarchar(100)
)
BEGIN
set #total=(SELECT sum(Total_Crédit) from credit_electricité) ;
if event=='ajouter'
and newtournee is null
THEN
if #total-newmontant>=0
INSERT into vignetteoneep VALUES(newidpiece,newnpiece,newnop,newdateengagement,
newdatefacture,newlocalité,newtournee,newnpolice,newservice,newmontant,newecheance,newcreated);
then
UPDATE credit_electricité set Total_Crédit=Total_Crédit-newmontant;
end if;
end if;
END
create PROCEDURE PR_Credit_Total(
#newidpiece AS int,
#newnpiece AS bigint,
#newnop AS varchar(60),
#newdateengagement AS date,
#newdatefacture AS date,
#newlocalité AS varchar(50),
#newtournee AS int,
#newnpolice AS bigint,
#newservice AS varchar(20),
#newmontant AS float,
#newecheance AS varchar(7),
#newcreated AS varchar(25),
#event AS varchar(100),
#total AS INT
)
AS
BEGIN
set #total=(SELECT sum(Total_Crédit) from credit_electricité)
if #event = 'ajouter' and #newtournee is null
if #total - #newmontant >=0
INSERT into vignetteoneep VALUES(newidpiece,newnpiece,newnop,newdateengagement,
newdatefacture,newlocalité,newtournee,newnpolice,newservice,newmontant,newecheance,newcreated)
UPDATE credit_electricité set Total_Crédit=Total_Crédit - #newmontant
END
Here's a list of syntax errors to fix
in eventvarchar(100) missing space ,should be in event varchar(100) but event is a keyword and best avoided
if event=='ajouter' null safe equal in mysql is <=> so should be if event <=> 'ajouter' but = would do
The second if does not have a then it's not clear if the insert and update should be part of same condition but I suspect not
if #total-newmontant>=0 then
INSERT ...
else
UPDATE ...
end if;
And you may need to set delimiters
I'm using the following stored-procedure to update a table:
DELIMITER $$
CREATE DEFINER=`developer`#`localhost` PROCEDURE `update_patient`(IN `patient_id` INT(11), IN `name` VARCHAR(45), IN `surname` VARCHAR(45), IN `middle_name` VARCHAR(45), IN `email` VARCHAR(45), IN `phone` VARCHAR(45), IN `mobile` VARCHAR(45), IN `address_id` INT(11), IN `address_no` VARCHAR(8), IN `ID` VARCHAR(45), IN `DOB` DATE)
NO SQL
UPDATE
patient
SET name = name,
surname = surname,
middle_name = middle_name,
email = email,
phone = phone,
mobile = mobile,
address_id = address_id,
address_no = address_no,
ID = ID,
DOB = DOB
WHERE
patient_id = patient_id
LIMIT 1;
END$$
DELIMITER ;
When I'm trying to call it through phpmyadmin I get the error: #1062 - Duplicate entry '844844' for key 'ID_UNIQUE'
844844 refers to ID field. I have this field in patient table and I want to update the patient's data. However, the primary key of patient table is patiend_id and not ID.
Do you know how to fix the error?
The problem is your input params for the Stored procedure are same as your column names in the table. This is leading to ambiguous behaviour.
Eg: In SET name = name ; how does MySQL resolve which one of this is the param value and which one is the column name ?
I generally prefix IN params with in_ and OUT with out_ for code readability and avoiding ambiguous behaviour.
DELIMITER $$
CREATE definer=`developer`#`localhost`
PROCEDURE `update_patient`(IN `in_patient_id` INT(11),
IN `in_name` VARCHAR(45),
IN `in_surname` VARCHAR(45),
IN `in_middle_name` VARCHAR(45),
IN `in_email` VARCHAR(45),
IN `in_phone` VARCHAR(45),
IN `in_mobile` VARCHAR(45),
IN `in_address_id` INT(11),
IN `in_address_no` VARCHAR(8),
IN `in_id` VARCHAR(45),
IN `in_dob` date)
NO SQL
UPDATE patient
SET name = in_name,
surname = in_surname,
middle_name = in_middle_name,
email = in_email,
phone = in_phone,
mobile = in_mobile,
address_id = in_address_id,
address_no = in_address_no,
id = in_id,
dob = in_dob
WHERE patient_id = in_patient_id
LIMIT 1;
END$$
DELIMITER ;
In my Mysql , I have written below stored procedure to insert data into user table,
DELIMITER $$
DROP PROCEDURE IF EXISTS `CreateUser1`$$
CREATE PROCEDURE `CreateUser1`(
IN Email VARCHAR(50),
IN Password1 VARCHAR(50),
IN FirstName VARCHAR(50),
IN LastName VARCHAR(50),
IN AlternateEmail VARCHAR(50),
IN PhoneNumber VARCHAR(50),
IN Token VARCHAR(500)
)
BEGIN
IF NOT EXISTS( SELECT user_id FROM `um.user` WHERE `email`=Email)THEN
INSERT INTO `um.user`(site_id,email,PASSWORD,alternate_email,first_name,last_name,contact_number,
created_on,updated_on,is_active,token,is_verified_email)
VALUES
(1, Email1 , Password1 ,AlternateEmail, FirstName , LastName ,PhoneNumber,UTC_TIMESTAMP(),UTC_TIMESTAMP(),1,Token,0);
END IF;
END$$
DELIMITER ;
When i test this procedure as below,
CALL `CreateUser1`('ab1#ansys.com' , 'abcdefgh' ,'abc#gmail.com', 'sa' , '' ,'123456789','hasghsdfhgfhgfhdgfhdsgsh');
SELECT * FROM `um.user` WHERE email='ab1#ansys.com';
It does nothing.
It doesn't insert data into table, I figured out the issue .
The isssue is in parameter "Email".
But when I change the parameter "Email" to "Email12" , it worked as expected.
But I don't want to change in parameter as it will be a change in my API as well,
Now i want to solve this issue in sp level as well, I have tried below changes as well in SP which also doesn't works,
Set #userEmail=Email;
IF NOT EXISTS( SELECT user_id FROM `um.user` WHERE `email`=#userEmail)THEN
Any suggestions
Regards
Sangeetha
You may also qualify their identifiers. See 9.2.1 Identifier Qualifiers.
...
/*
-- You can also use Alias
SELECT `user_id`
FROM `um.user` `uu`
WHERE `uu`.`email` = `Email`
*/
IF NOT EXISTS (SELECT `user_id`
FROM `um.user`
WHERE `um.user`.`email` = `Email`) THEN
...
SQL Fiddle demo unqualified
SQL Fiddle demo qualified
Try this if you want to handle your issue within the stored procedure itself, without changing the name of parameter,
DELIMITER $$
DROP PROCEDURE IF EXISTS `CreateUser1`$$
CREATE PROCEDURE `CreateUser1`(
IN Email VARCHAR(50),
IN Password1 VARCHAR(50),
IN FirstName VARCHAR(50),
IN LastName VARCHAR(50),
IN AlternateEmail VARCHAR(50),
IN PhoneNumber VARCHAR(50),
IN Token VARCHAR(500)
)
BEGIN
DECLARE Email1 VARCHAR(50);
SET Email1 = Email;
IF NOT EXISTS( SELECT user_id FROM `um.user` WHERE `email`=Email1)THEN
INSERT INTO `um.user`(site_id,email,PASSWORD,alternate_email,first_name,last_name,contact_number,
created_on,updated_on,is_active,token,is_verified_email)
VALUES
(1, Email1 , Password1 ,AlternateEmail, FirstName , LastName ,PhoneNumber,UTC_TIMESTAMP(),UTC_TIMESTAMP(),1,Token,0);
END IF;
END$$
DELIMITER ;
This is because in MySQL, the parameter name passed to a Stored Procedure should not be the same as Column Name. Hope this solves your problem :)
I am running following query but it is showing me an 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 'end' at line 18
DELIMITER //
create procedure usp_ItemAdd(
p_itemname varchar(50),
p_company varchar(50),
p_model varchar(50),
p_unit varchar(10),
p_photo varchar(12),
p_color varchar(50),
p_size varchar(50),
p_weight varchar(20),
p_stock varchar(20)
)
begin
insert into item_tbl(
item_name,company,model_no,unit,photo,color,size,weight,stock
)
values(
p_itemname,p_company,p_model,p_unit,p_photo,p_color,p_size,p_weight,p_stock
)
end //
DELIMITER ;
Add delimiter and finish insert statement with ;
delimiter $$
create procedure usp_ItemAdd(
p_itemname varchar(50),
p_company varchar(50),
p_model varchar(50),
p_unit varchar(10),
p_photo varchar(12),
p_color varchar(50),
p_size varchar(50),
p_weight varchar(20),
p_stock varchar(20)
)
begin
insert into item_tbl(
item_name,company,model_no,unit,photo,color,size,weight,stock
)
values(
p_itemname,p_company,p_model,p_unit,p_photo,p_color,p_size,p_weight,p_stock
);
end $$
delimiter ;
I created a procedure that is supposed to do some operation but every time I call it, mysql comes out with an error, which I have not clue what it means. I have tried to understand it in vain, here 's the table structure which the stored procedure where made to do an operation on it:
CREATE TABLE `recruitment`.`job_seeker` (
`user_id` INT Null ,
`fname` VARCHAR(45) Null ,
`lname` VARCHAR(45) Null ,
`mname` VARCHAR(45) Null ,
`gender` VARCHAR(10) Null ,
`dob` DATE Null ,
`marital_status` VARCHAR(45) Null ,
`address` VARCHAR(45) Null ,
`city` VARCHAR(45) Null ,
`nationality` VARCHAR(45) Null ,
`phone` VARCHAR(45) Null ,
`mobile` VARCHAR(45) Null ,
`degree_id` INT Null ,
`education` VARCHAR(100) Null ,
`experience` VARCHAR(250) Null ,
`other` VARCHAR(250) Null ,
`job_target` VARCHAR(250) Null ,
PRIMARY KEY (`user_id`) ,
INDEX `user_id` (`user_id` ASC) ,
INDEX `degree_id` (`degree_id` ASC) ,
CONSTRAINT `user_id`
FOREIGN KEY (`user_id` )
REFERENCES `recruitment`.`user_authentication` (`user_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `degree_id`
FOREIGN KEY (`degree_id` )
REFERENCES `recruitment`.`degree` (`degree_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
Here's the stored procedure:
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `createSeekerProfile`(in userName varchar(45),
in fn varchar(45),in mn varchar(45), in ln varchar(45),
in gender varchar(6),in nationality varchar(45),
in ad varchar(45),in city varchar(45),in phone varchar(15),in mob varchar(15),
in maritalStatus varchar(45), in degId int, in educ varchar(100),
in exper varchar(250), in other varchar(250),
in dob date,in jtarg varchar(250))
begin
declare returned_ID int;
set #dyn_que = CONCAT('select user_id into #returned_ID
from user_authentication where user_name = ? ');
prepare s1 from #dyn_que ;
set #usn = userName;
execute s1 using #usn ;
set #dyn_update =CONCAT('update job_seeker set fname =',fn,', lname = ',ln,' ,mname = ',mn,' ,
nationality =',nationality,',address =',ad,',city =',city,',phone=',phone,',
mobile =',mob,', gender = ',gender,',other =',other,',
degree_id =',degId,', job_target=',jtarg,', dob =',dob,', education =',educ,',
experience=',exper,', marital_status=',maritalStatus,' where user_id =#returned_ID');
prepare s2 from #dyn_update;
execute s2;
end
Whenever I call the procedure through:
call createSeekerProfile('realsilhouette','robert','marie','david','male'
,'earthal','an address here','capital of earth','012178152',
'1111111111','single',2,'engineering','looking forward','determined',
'2008-7-04','Oracle CEO')
I get an awful error which 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 'address here,
city =capital of earth,phone=012178152,
mobile =1111111111, gender' at line 2
However when I try execute the update statement manually which is inside stored procedure, it works out so well.
new Post :
thanks god,finally I fixed the problem up, The problem came down to the order, all I did was just make the parameters in order, though update statement does not care about the order, as far as I know, I'm not sure, But what i am sure of is the stored procedure got created beautifully, here's the new code :
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `createSeekerProfile`(
in un varchar(45),
in fn varchar(45),
in ln varchar(45),
in mn varchar(45),
in g varchar(10),
in dateOfBirth date,
in ms varchar(45),
in ad varchar(45),
in city varchar(45),
in nat varchar(45),
in ph varchar(45),
in mob varchar(45),
in degid int,
in educ varchar(100),
in exp varchar(250),
in other varchar(250),
in jtarg varchar(250))
begin
declare returned_ID int(11);
set #dyn_que = CONCAT('select user_id into #returned_ID from user_authentication
where user_name = ? ');
prepare s1 from #dyn_que ;
set #usn = un;
execute s1 using #usn ;
set #dyn_update =CONCAT('update job_seeker set fname
="',fn,'",lname="',ln,'",mname="',mn,'",lname ="',ln,'",
gender ="',g,'",dob="',dateOfBirth,'",marital_status="',ms,'",
address="',ad,'",city="',city,'",
nationality="',nat,'",phone="',ph,'",mobile="',mob,'",degree_id="',
degid,'",education="',educ,'",
experience="',exp,'",other="',other,'",job_target="',jtarg,'"
where user_id = #returned_ID');
prepare stm from #dyn_update;
execute stm;
end $$
many thanks
Your forgot about quoting string values.
for example:
concat('update yourTable
set address ="', #address, '"
where id = 1');
or use function quote
For me adding character ` (GRAVE ACCENT) helped. It is not a single quote (')
CREATE TABLE Order
(
Order_Id integer NOT NULL ,
Order_Time datetime NULL ,
Order_Status char(50) NULL ,
Customer_Id integer NOT NULL
)