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 ;
Related
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 ;
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'm new to mysql and I'm trying to create a stored procedure but getting syntax error due to declare a variable.
The version of mysql I'm using is 5.5.43 and my procedure is:
CREATE PROCEDURE spSearch
(
p_ACTION INT,
p_PROJECT_CUSTOMER INT,
p_PROJECT_NAME VARCHAR(50),
p_PROJECT_SALESPERSON INT,
p_PROJECT_MANAGER INT,
p_PROJECT_PMF INT,
p_PROJECT_TYPE INT,
p_PROJECT_DESIGNER INT,
p_PROJECT_AANDD INT,
p_PROJECT_REFD INT,
p_PROJECT_VENDOR INT,
p_PROJECT_GENERALCONTRACTOR INT,
p_PROJECT_PUNCHLIST varchar(1),
p_PROJECT_LOCATION INT,
p_PROJECT_CLIENTTYPE INT,
p_PROJECT_INSTALLDATE VARCHAR(50),
p_PROJECT_INSTALLDATETO VARCHAR(50),
p_PROJECT_ORDERENTRYDATE VARCHAR(50),
p_PROJECT_ORDERENTRYDATETO VARCHAR(50),
p_PROJECT_MOVEINDATE VARCHAR(50),
p_PROJECT_MOVEINDATETO VARCHAR(50),
p_PROJECT_ACTIVE_FLAG VARCHAR(1)
)
BEGIN
DECLARE ABC VARCHAR(1400);
END;
I'm getting this 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 28
Please help to remove this syntax error.
It seems that you have a problem with your delimiters. try this:
delimiter //
CREATE PROCEDURE spSearch
(
p_ACTION INT,
p_PROJECT_CUSTOMER INT,
p_PROJECT_NAME VARCHAR(50),
p_PROJECT_SALESPERSON INT,
p_PROJECT_MANAGER INT,
p_PROJECT_PMF INT,
p_PROJECT_TYPE INT,
p_PROJECT_DESIGNER INT,
p_PROJECT_AANDD INT,
p_PROJECT_REFD INT,
p_PROJECT_VENDOR INT,
p_PROJECT_GENERALCONTRACTOR INT,
p_PROJECT_PUNCHLIST varchar(1),
p_PROJECT_LOCATION INT,
p_PROJECT_CLIENTTYPE INT,
p_PROJECT_INSTALLDATE VARCHAR(50),
p_PROJECT_INSTALLDATETO VARCHAR(50),
p_PROJECT_ORDERENTRYDATE VARCHAR(50),
p_PROJECT_ORDERENTRYDATETO VARCHAR(50),
p_PROJECT_MOVEINDATE VARCHAR(50),
p_PROJECT_MOVEINDATETO VARCHAR(50),
p_PROJECT_ACTIVE_FLAG VARCHAR(1)
)
BEGIN
DECLARE ABC VARCHAR(1400);
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 */
ERROR 1064 (42000): 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 '= (se
lect ifnull(max(manufactureId+1),1) from tbl_Manufacturer
) ;
create procedure ManufactureAdd(
p_manufactureName longtext,
p_address longtext,
p_phone varchar(50),
p_email varchar(50),
p_description longtext
)
begin
declare p_manufactureId = (select ifnull(max(manufactureId+1),1) from tbl_Manufacturer
) ;
insert into tbl_Manufacturer(
manufactureId,
manufactureName,
address,
phone,
email,
description
)
VALUES
(
p_manufactureId,
p_manufactureName,
p_address,
p_phone,
p_email,
p_description
) ;
SELECT p_manufactureId ;
end
Try to use SELECT..INTO clause and simple user variable -
CREATE PROCEDURE ManufactureAdd(
p_manufactureName longtext,
p_address longtext,
p_phone varchar(50),
p_email varchar(50),
p_description longtext)
BEGIN
SELECT IFNULL(MAX(manufactureId + 1), 1) INTO #p_manufactureId FROM tbl_Manufacturer;
INSERT INTO tbl_Manufacturer (manufactureId, manufactureName, address, phone, email, DESCRIPTION)
VALUES (#p_manufactureId, p_manufactureName, p_address, p_phone, p_email, p_description);
SELECT #p_manufactureId;
END
Try this:
CREATE PROCEDURE ManufactureAdd(
p_manufactureName LONGTEXT,
p_address LONGTEXT,
p_phone VARCHAR(50),
p_email VARCHAR(50),
p_description LONGTEXT
)
BEGIN
DECLARE p_manufactureId INT;
SELECT IFNULL(MAX(manufactureId+1),1) INTO p_manufactureId FROM tbl_Manufacturer;
INSERT INTO tbl_Manufacturer(
manufactureId, manufactureName, address, phone, email, description
)
VALUES (
p_manufactureId, p_manufactureName, p_address, p_phone, p_email, p_description
);
SELECT p_manufactureId ;
END