I have unsloved problem when I try to create a stored procedure following code below
but it shows 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
and this is my code Thanks for advice.
CREATE DEFINER=root#localhost PROCEDURE item_page_insert(IN cid INT, IN it_title VARCHAR(200), IN tumbnail VARCHAR(300), IN publish_date DATE, IN cover_set VARCHAR(20), IN pcontent TEXT, IN status INT) MODIFIES SQL DATA
BEGIN
DECLARE mpid INT;
SELECT max(pid)+1 INTO mpid
FROM tbpage;
INSERT INTO tbite(cid, pid, it_title, tumbnail, publish_date, cover_set)
VALUES(cid,
mpid,
it_title,
tumbnail,
publish_date,
cover_set);
INSERT INTO tbpage(pid, pcontent, set_date, status)
VALUES(mpid,
pcontent,
now(),
status); END;
DELIMITER ;
it should be
DECLARE mpid AS INT;
try that one
Related
I'm trying to run this query on MySQL server:
CREATE PROCEDURE forum.eventlog_create(
i_UserID INT,
i_Source VARCHAR(128),
i_Description TEXT,
i_Type INT,
i_UTCTIMESTAMP DATETIME)
MODIFIES SQL DATA
BEGIN
INSERT INTO forum.EventLog
(UserID, Source, Description, ´Type´)
VALUES (i_UserID, i_Source, i_Description, i_Type);
END;
However upon executing it I get the following error:
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
and I'm unable to fix it. I tried to search for a solution and asked a co-worker but we were unable to find the solution, as last resort I decided to ask it here.
I get error code 1064 but the right syntax near '' is the message and I dont understand what the problem could be. It would be easier if it said which syntax gives the error, I only get the line number.
Thank you for your time
There is one error caused by the escape character around type which should be either backticks or dropped and you should try setting delimiters https://dev.mysql.com/doc/refman/5.7/en/stored-programs-defining.html
delimiter $$
CREATE PROCEDURE eventlog_create(
i_UserID INT,
i_Source VARCHAR(128),
i_Description TEXT,
i_Type INT,
i_UTCTIMESTAMP DATETIME)
MODIFIES SQL DATA
BEGIN
INSERT INTO forum.EventLog
(UserID, Source, Description, `Type`)
VALUES (i_UserID, i_Source, i_Description, i_Type);
END $$
delimiter ;
Try this
DELIMITER $$
CREATE PROCEDURE forumeventlog_create()
BEGIN
declare i_UserID INT DEFAULT 0;
declare i_Source VARCHAR(128) DEFAULT null;
declare i_Description TEXT DEFAULT null;
declare i_Type INT DEFAULT 0;
declare i_UTCTIMESTAMP DATETIME DEFAULT null ;
INSERT INTO forum.EventLog
(UserID, Source, Description, ´Type´)
VALUES (i_UserID, i_Source, i_Description, i_Type);
END $$
DELIMITER ;
You can call this by
CALL forumeventlog_create()
OR
DELIMITER $$
CREATE PROCEDURE forumeventlog_create(i_UserID INT,i_Source VARCHAR(128),i_Description TEXT,i_Type INT,i_UTCTIMESTAMP DATETIME)
BEGIN
INSERT INTO forum.EventLog
(UserID, Source, Description, ´Type´)
VALUES (i_UserID, i_Source, i_Description, i_Type);
END $$
DELIMITER ;
Getting error during Creating Stored Procedure for Callable Statement:
I know some thing very simple going wrong, but i'm just unable to figure out!
My QUERY:
USE demo;
1. CREATE PROCEDURE
2. INSERT_emp_data (IN ID INT, IN NAME VARCHAR(2), IN AGE INT, IN IMAGE BLOB)
3. BEGIN
4. INSERT INTO emp_data VALUES(ID, NAME, AGE, IMAGE);
5. END;
/
SQL query:
CREATE PROCEDURE
INSERT_emp_data (IN ID INT, IN NAME VARCHAR(2), IN AGE INT, IN IMAGE BLOB)
BEGIN
INSERT INTO emp_data VALUES(ID, NAME, AGE, IMAGE);
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 '' at line 4
Appreciate your help!
Thank for your time!
When you write a stored procedure in MySQL, you should use the DELIMITER statement. In addition, you should name your columns so they do not conflict with column names. And, when using INSERT always list the columns name. So:
DELIMITER $$
CREATE PROCEDURE INSERT_emp_data (
IN IN_ID INT,
IN IN_NAME VARCHAR(2),
IN IN_AGE INT,
IN IN_IMAGE BLOB
)
BEGIN
INSERT INTO emp_data(id, name, age, image)
VALUES(IN_ID, IN_NAME, IN_AGE, IN_IMAGE);
END;
$$
DELIMITER ;
I am new to stored procedure
DELIMITER //
CREATE PROCEDURE sp_MyNewTable
(IN Mod nvarchar(50),IN Did int,IN startdate datetime,IN enddate datetime)
BEGIN
Declare DateDuration int,
SET actstatus=1,
SET DateDuration = SELECT DATEDIFF(startdate,enddate) as Datediff
insert into mytable (Duration,Module,Deptid,taskstartdate,activestatus) values (DateDuration,Mod,did,enddate,startdate,actstatus)
Select * from mytable
END //
DELIMITER;
Getting error if I execute this:
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 sp_MyNewTable (IN Mod nvarchar(50),IN Did int,IN startdate
datetime,IN enddate datetime)
MYSQL version is MYSQL 5.1
Try:
DELIMITER //
CREATE PROCEDURE `sp_MyNewTable`(IN `Mod` VARCHAR(50),
IN `Did` INT,
IN `startdate` DATETIME,
IN `enddate` DATETIME)
BEGIN
DECLARE `DateDuration` INT;
DECLARE `actstatus` DATETIME;
SET `actstatus` := 1, `DateDuration` := DATEDIFF(`startdate`, `enddate`);
INSERT INTO `mytable`
(`Duration`, `Module`, `Deptid`, `taskenddate`, `taskstartdate`, `activestatus`)
VALUES
(`DateDuration`, `Mod`, `Did`, `enddate`, `startdate`, `actstatus`);
SELECT * FROM `mytable`;
END//
DELIMITER ;
SQL Fiddle demo
I'm trying to add the following stored procedure to my mysql database:
CREATE PROCEDURE logmsg ( _Username VARCHAR(50), _Message VARCHAR(80) )
BEGIN
INSERT INTO chatlogs (sender, message) VALUES (_Username, _Message);
END;
But its failing the query and returning:
#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 3
I've been searching for about 2 hours on google and cannot find any answer at all.
Any help is greatly appreciated!
While I'm not 100% sure because I can't test on a MySQL server at the moment, I think the problem is in the semicolon. On the line with INSERT you basically end the CREATE PROCEDURE statement, which has incorrect syntax this way. You have to set the delimiter to something else (e.g. //), to be able to use the semicolon in the body of the procedure:
delimiter //
CREATE PROCEDURE logmsg ( _Username VARCHAR(50), _Message VARCHAR(80) )
BEGIN
INSERT INTO chatlogs (sender, message) VALUES (_Username, _Message);
END//
delimiter ;
DELIMITER $$
DROP PROCEDURE IF EXISTS quotations.sp_addservices $$
CREATE PROCEDURE quotations.sp_addservices
(In categoryname varchar(25),in servicename varchar(250),in hours float,in cost float,in basis nvarchar (100))
BEGIN
insert into categorydetails (Category_Name) values (categoryname);
if(categoryname!=null)
then
DECLARE category_id int;
set category_id= select max(Category_Id) from categorydetails ;
insert into servicesdetails (Service_Name,Category_Id,Hours,Cost,Basis) values(servicename,category_id,hours,cost,basis);
end if;
END $$
DELIMITER ;
This is my stored procedure .I have to retrive the value of categoryid that is posted into the database which is auto increased.Here i cant declare the variable and assign value to variable.Am getting error like
Script line: 4 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 'DECLARE category_id int;
set category_id= select max(Category_Id) from categor' at line 9
Can any one help me
Thanks in advance.
Try
SELECT MAX(c.category_id) INTO category_id FROM categorydetails c;