Related
I have this table:
User
user_id int PK
username varchar(20)
secret_code varchar(20)
name varchar(20)
age int
gender varchar(20)
city varchar(20)
latest_signin_time timestamp
latest_signout_time timestamp
loc_list json
buddy_list json
I created a stored procedure:
create procedure insert_users(IN user_id int , in username varchar(20),in secret_code varchar(20),
in name varchar(20), in age int, in gender varchar(20), in city varchar(20),
in latest_signin_time timestamp, in latest_signout_time timestamp,
in loc_list json , in buddy_list json)
begin
insert into user values(user_id, username, secret_code, name, age, gender, city,
latest_signin_time, latest_signout_time, loc_list,buddy_list)
end ;
call insert_user('1', 'avs431','pwd1','Ameya','22','Male','Mumbai',null,null,'[]','[]');
However, my code isn't running and I keep 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 'end' at line 9"
I'm not sure what is going wrong here. Can anyone help?
Thank you!
create procedure insert_users(IN user_id int , in username varchar(20),in secret_code varchar(20),
in name varchar(20), in age int, in gender varchar(20), in city varchar(20),
in latest_signin_time timestamp, in latest_signout_time timestamp,
in loc_list json , in buddy_list json)
/* begin */
insert into user values(user_id, username, secret_code, name, age, gender, city,
latest_signin_time, latest_signout_time, loc_list,buddy_list)
/* end */ ;
The commands in procedure terminate with semicolon (;). Use prefixes in the procedure parameters so the parameters won't be mixed up with column names. Also, list the column names in INSERT so your procedure will work even if a column is added to the table.
delimiter //
create procedure insert_users(
in_user_id int,
in_username varchar(20),
in_secret_code varchar(20),
in_name varchar(20),
in_age int,
in_gender varchar(20),
in_city varchar(20),
in_latest_signin_time timestamp,
in_latest_signout_time timestamp,
in_loc_list json,
in_buddy_list json
)
begin
insert into user (user_id, username, secret_code, name, age, gender,
city, latest_signtime, latest_signout_time, loc_list, buddy_list)
values(in_user_id, in_username, in_secret_code, in_name, in_age, in_gender,
in_city, in_latest_signin_time, in_latest_signout_time, in_loc_list, in_buddy_list);
end
//
delimiter ;
call insert_user('1', 'avs431','pwd1','Ameya','22','Male','Mumbai',null,null,'[]','[]');
This question already has answers here:
How can I fix MySQL error #1064?
(3 answers)
Closed 4 years ago.
I have the following stored procedure in mysql.
CREATE DEFINER=`cefcy`#`localhost` PROCEDURE `insert_eD_medication` (IN `p_id` INT(11), IN `strength` VARCHAR(45), IN `unit_id` INT(11), IN `package_no` VARCHAR(45), IN `medicinal_product_package` VARCHAR(45), IN `pharmaceutical_dose_form_id` INT(11), IN `brandName` VARCHAR(45), IN `route_of_administration_id` INT(11), IN `date_of_dispense` DATE, IN `substitution` VARCHAR(45), IN `stat` TINYINT(4), IN `v_id` INT(11))
NO SQL
BEGIN
INSERT INTO `medication_dispensation_mapping` (`patient_id`, `strength`, `unit_id`, `package_no`, `medicinal_product_package`, `pharmaceutical_dose_form_id`,`brandName`, `route_of_administration_id`, `date_of_dispense_medicine_event`, `substitution`, `status`, `version_id`)
VALUES (p_id, strength, unit_id, package_no, medicinal_product_package, pharmaceutical_dose_form_id, brandName, route_of_administration_id, date_of_dispense, substitution, stat, v_id);
END
When I'm trying to run it, I get 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 5
Can you help me please?
Thanks to #DanFromGermany comment and the proposed How can I fix MySQL error #1064?, I found the solution.
Even thought I haven't changed the delimiter I had to define it in the stored procedure.
Thus, my correct code is:
DELIMITER $$
CREATE DEFINER=`cefcy`#`localhost` PROCEDURE `insert_eD_medication` (IN `p_id` INT(11), IN `strength` VARCHAR(45), IN `unit_id` INT(11), IN `package_no` VARCHAR(45), IN `medicinal_product_package` VARCHAR(45), IN `pharmaceutical_dose_form_id` INT(11), IN `brandName` VARCHAR(45), IN `route_of_administration_id` INT(11), IN `date_of_dispense` DATE, IN `substitution` VARCHAR(45), IN `stat` TINYINT(4), IN `v_id` INT(11))
NO SQL
BEGIN
INSERT INTO `medication_dispensation_mapping` (`patient_id`, `strength`, `unit_id`, `package_no`, `medicinal_product_package`, `pharmaceutical_dose_form_id`,`brandName`, `route_of_administration_id`, `date_of_dispense_medicinal_event`, `substitution`, `status`, `version_id`)
VALUES (p_id, strength, unit_id, package_no, medicinal_product_package, pharmaceutical_dose_form_id, brandName, route_of_administration_id, date_of_dispense, substitution, stat, v_id);
END;
$$
DELIMITER ;
I created a stored procedure which should perform the following:
Insert into Agencies Table, creating an id in the PK index row
Save the id in a variable
Insert the id and other data into Users table
The syntax below does not trigger any errors:
DELIMITER $$
CREATE PROCEDURE insertNewAgencyAndAdmin (IN aName varchar (100), IN numTrav int, IN polType int, IN uEmail varchar(255), IN uFName varchar(40), IN uLName varchar(40), IN uTitle varchar(100))
BEGIN
INSERT INTO btsAgency.Agencies (agencyName, numTrav, polType) VALUES (#aName, #numTrav, #polType);
SET #agencyID = (SELECT agencyID from btsAgency.Agencies where agencyID = LAST_INSERT_ID());
INSERT INTO btsUsers.Users (userAgencyID, userEmail, userFirstName, userLastName, userTitle ) VALUES
(#agencyID, #uEmail, #uFName, #uLName, #uTitle);
END
However, the Stored Proc doesn't insert my parameters into the tables when executed. So, after searching I try to create the SP like this (including $$ after "END" and resetting the delimiter to a semi-colon ):
DELIMITER $$
CREATE PROCEDURE insertNewAgencyAndAdmin (IN aName varchar (100), IN numTrav int, IN polType int, IN uEmail varchar(255), IN uFName varchar(40), IN uLName varchar(40), IN uTitle varchar(100))
BEGIN
INSERT INTO btsAgency.Agencies (agencyName, numTrav, polType) VALUES (#aName, #numTrav, #polType);
SET #agencyID = (SELECT agencyID from btsAgency.Agencies where agencyID = LAST_INSERT_ID());
INSERT INTO btsUsers.Users (userAgencyID, userEmail, userFirstName, userLastName, userTitle ) VALUES
(#agencyID, #uEmail, #uFName, #uLName, #uTitle);
END $$
DELIMITER ;
MySql creates the Stored Proc but gives me 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 'DELIMITER' at line 1
When I execute the created Stored Proc it still doesn't insert the data in my parameters.
Any help would be appreciated.
Some problems in your stored procedure:
It is important to indicate the difference between 9.4. User-Defined Variables and routine parameters 13.1.15. CREATE PROCEDURE and CREATE FUNCTION Syntax, are different variables (eg.: aName != #aName).
Avoid naming parameters or variables as columns of your tables.
DELIMITER $$
CREATE PROCEDURE `insertNewAgencyAndAdmin` (
/*
IN `aName` varchar (100),
IN `numTrav` int,
IN `polType` int,
IN `uEmail` varchar(255),
IN `uFName` varchar(40),
IN `uLName` varchar(40),
IN `uTitle` varchar(100)
*/
IN `_aName` varchar (100),
IN `_numTrav` int,
IN `_polType` int,
IN `_uEmail` varchar(255),
IN `_uFName` varchar(40),
IN `_uLName` varchar(40),
IN `_uTitle` varchar(100)
)
BEGIN
/*
INSERT INTO `btsAgency`.`Agencies` (
`agencyName`,
`numTrav`,
`polType`
) VALUES (
#`aName`,
#`numTrav`,
#`polType`
);
*/
INSERT INTO `btsAgency`.`Agencies` (
`agencyName`,
`numTrav`,
`polType`
) VALUES (
`_aName`,
`_numTrav`,
`_polType`
);
/*
SET #`agencyID` = (SELECT `agencyID` from `btsAgency`.`Agencies` where `agencyID` = LAST_INSERT_ID());
INSERT INTO `btsUsers`.`Users` (
`userAgencyID`,
`userEmail`,
`userFirstName`,
`userLastName`,
`userTitle`
) VALUES (
#`agencyID`,
#`uEmail`,
#`uFName`,
#`uLName`,
#`uTitle`
);
*/
INSERT INTO `btsUsers`.`Users` (
`userAgencyID`,
`userEmail`,
`userFirstName`,
`userLastName`,
`userTitle`
) VALUES (
LAST_INSERT_ID(),
`_uEmail`,
`_uFName`,
`_uLName`,
`_uTitle`);
END$$
DELIMITER ;
SELECT * FROM discounts $$
delimiter $$;
CREATE PROCEDURE insertData(IN id int,IN title varchar(255),IN amount decimal)
BEGIN
insert into discounts values (id,title,amount);
END; $$
call insertData(3,"sadasd",56);
/*DROP PROCEDURE insertData */
I'm actually trying to insert value into table using a procedure.This is my table code
create table employeeTable(
employeeId varchar(20),
employeeName varchar(20),
qualification varchar(20),
cadre varchar(15),
age numeric(3),
sex char(1),
occupation varchar(10),
dateOfBirth date,
address varchar(50),
city varchar(15),
employeePhoto blob,
constraint pk_employeeId_employeeTable primary key(employeeId)
);
My insert procedure is
delimiter //
create procedure insertEmployee(in employeeId varchar(20),in employeeName varchar(20),
in qualification varchar(20),in cadre varchar(15),in age numeric(3),in sex char(1),
in occupation varchar(10),in dateOfBirth date,in address varchar(50),in city varchar(15),
in employeePhoto blob)
begin
insert into employeetable values(upper(employeeId),employeeName,qualification,
cadre,age,sex,occupation,
str_to_date(dateOfBirth,'%d-%m-%Y'),address,city,employeePhoto);
end;//
delimiter ;
And I'm trying to call this procedure with following parameters
call insertEmployee('A001','Murugan','Phd','Manager',12,'M','service','30-07-1994',
'30,PNG nagar,pollachi','Coimbatore',null);
But values are not getting inserted and it's showing an error #1048 - Column 'dateOfBirth' cannot be null..
What is the problem here??
delimiter //
create procedure insertEmployee(in employeeId varchar(20),in employeeName varchar(20),
in qualification varchar(20),in cadre varchar(15),in age numeric(3),in sex char(1),
in occupation varchar(10),in dateOfBirth varchar(15),in address varchar(50),in city varchar(15),
in employeePhoto blob)
begin
insert into employeetable values(upper(employeeId),employeeName,qualification,
cadre,age,sex,occupation,
str_to_date(dateOfBirth,'%d-%m-%Y'),address,city,employeePhoto);
end;//
delimiter ;
Try this procedure. You were accpeting the dateOfBirth as a date object in the parameter list. It should have been a VARCHAR as that is how it is passed to the procedure.
create procedure sp_FldBkgData
(
IN awb int,
IN origin varchar(2),
IN dist varchar(2),
IN courier int,
IN pdate varchar(10),
IN acc int,
IN service varchar(1),
IN agent1 varchar(5),
IN agent2 varchar(5),
IN bkgtime int,
IN sname varchar(45),
IN sadd1 varchar(45),
IN sadd2 varchar(45),
IN sadd3 varchar(45),
IN scity varchar(45),
IN sstate varchar(25),
IN spostal int,
IN scountry varchar(8),
IN sph varchar(11),
IN sfax varchar(25),
IN smail varchar(45),
IN sref varchar(45),
IN carrier varchar(45),
IN ccompany varchar(45),
IN cname varchar(45),
IN cadd1 varchar(45),
IN cadd2 varchar(45),
IN cadd3 varchar(45),
IN ccity varchar(25),
IN cstate varchar(15),
IN ccountry varchar(45),
IN cgateway varchar(3),
IN cph varchar(25),
IN goods varchar(45),
IN paymode varchar(7),
IN curr varchar(3),
IN val int,
IN pcs int,
IN weight double,
IN handling varchar(1),
IN insurrance varchar(3),
IN fac int,
IN gst int
)
BEGIN
INSERT INTO cms_module_fieldbooking (Awb) VALUES (awb);
END
Here is the error:
To create a procedure in the MySQL Workbench Query Editor, you need to specify a delimiter other than the semicolon.
For example:
DELIMITER $$
CREATE PROCEDURE foo()
BEGIN
SELECT 'foo';
END$$
DELIMITER ;
As an alternative, MySQL Workbench also has a "routine" editor that automatically handles the delimiter for you. (In the schema list on the left, right click on Procedures, and select "Create Procedure". When you click Apply, you'll see the actual script, that includes the DELIMITER statements.