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
Related
Stored Procedures syntax error at line 11 'BEGIN INSERT INTO customer(CUSTOMER_ID,NAME)'.............................................................................................................................................................................................................
BEGIN
IF EXISTS
(
SELECT
*
FROM
country
WHERE
country_code = #country_code
)
BEGIN
INSERT INTO customer(
CUSTOMER_ID,
NAME,
EMAIL,
COUNTRY_CODE,
BUDGET,
USED
)
VALUES(
#CUSTOMER_ID,
#NAME,
#EMAIL,
#COUNTRY_CODE,
#BUDGET,
#USED
)
END ELSE
BEGIN
INSERT INTO customer(
CUSTOMER_ID,
NAME,
EMAIL,
COUNTRY_CODE,
BUDGET,
USED
)
VALUES(
#CUSTOMER_ID,
#NAME,
#EMAIL,
#COUNTRY_CODE,
#BUDGET,
#USED
)
END
END
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `AddCustomer`(IN `CUSTOMER_ID` VARCHAR(4), IN `NAME` VARCHAR(50), IN `EMAIL` VARCHAR(50), IN `COUNTRY_CODE` VARCHAR(2), IN `COUNTRY_NAME` VARCHAR(50), IN `BUDGET` DECIMAL(18,2))
BEGIN
IF EXISTS
(
SELECT
*
FROM
country
WHERE
country_code = country_code
) THEN
INSERT INTO customer(
CUSTOMER_ID,
NAME,
EMAIL,
COUNTRY_CODE,
BUDGET,
USED
)
VALUES(
CUSTOMER_ID,
NAME,
EMAIL,
COUNTRY_CODE,
BUDGET,
USED
) ; ELSE
INSERT INTO country(COUNTRY_CODE, COUNTRY_NAME)
VALUES(COUNTRY_CODE, COUNTRY_NAME) ;
INSERT INTO customer(
CUSTOMER_ID,
NAME,
EMAIL,
COUNTRY_CODE,
BUDGET,
USED
)
VALUES(
CUSTOMER_ID,
NAME,
EMAIL,
COUNTRY_CODE,
BUDGET,
USED
) ;
END IF ;
END$$
DELIMITER ;
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 ;
I am trying to create a stored procedure and i am getting an error code.
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 '' at line 12
I'm not the greatest with MYSQL and I'm unsure where to even look to figure the problem out.
Create Procedure Sp_insertCustomer(
IN Customer_id VARCHAR(20) ,
IN UserName VARCHAR(20),
IN Fname VARCHAR(20),
IN Lname VARCHAR(20),
IN Dob Date,
IN Address VARCHAR(250),
IN Phone INT,
IN Email VARCHAR(250),
IN Ss VARCHAR(9) )
BEGIN
INSERT INTO Customer (Cusomter_id,UserName,Fname,Lname,Dob,Address,Phone,Email,Ss)
VALUES (in_Customer_id ,in_UserName , in_Fname , in_Lname , in_Dob , in_Address , in_Phone , in_Email , in_Ss);
END
1) Your parameters and values in insert statement are different:
Customer_id is parameter and in_Customer_id in insert statement
2) add delimeters
DELIMITER $$
< your procedure >
END$$ --- instead your END
DELIMITER ;
It seems like you missed the parameters names. Also you are not need to specify IN for params.
DELIMITER $$
Create Procedure Sp_insertCustomer(
Customer_id VARCHAR(20) ,
UserName VARCHAR(20),
Fname VARCHAR(20),
Lname VARCHAR(20),
Dob Date,
Address VARCHAR(250),
Phone INT,
Email VARCHAR(250),
Ss VARCHAR(9) )
BEGIN
INSERT INTO Customer (Cusomter_id, UserName, Fname, Lname, Dob, Address, Phone, Email, Ss)
VALUES (Customer_id, UserName, Fname, Lname, Dob, Address, Phone, Email, Ss);
END$$
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 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 ;