Error 1064 in a stored procedure in MySQL 8.0 - mysql

I am using MySQL Workbench 8.0 CE and i'm trying to create a stored procedure to show 2 fields from my table. I am receiving 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 3
This is my table:
CREATE TABLE student (
id INT PRIMARY KEY,
name VARCHAR(200),
age INT,
final_grade DOUBLE,
sex VARCHAR(1)
)
And this is the procedure:
CREATE PROCEDURE show_name_grade ()
BEGIN
SELECT name,final_grade FROM student;
END

You will need to redefine Delimiter to something else other than ;. At the end, define it back to ;
DELIMITER $$
CREATE PROCEDURE show_name_grade ()
BEGIN
SELECT name,final_grade FROM student;
END $$
DELIMITER ;

Related

Create Procedure to insert values into a table sql

I am attempting to create a procedure that will allow me to insert values into a table
the code I have is
Create procedure add_instructor
#Name char(40),
#IAccount char(20)
AS
BEGIN
set nocount on
insert into final_instructors (Name, IAccount)
values (#name, #IAccount)
end
go;
I am getting this error
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 '#Name char(40), #IAccount char(20)

MySQL Syntax Error With Stored Procedures

I must be missing something simple because I can't figure out what is causing my script to fail.
Below is the stored procedure I've written:
CREATE PROCEDURE `Search_contacts`(IN `in_owner_id` INT,
IN `in_first_name` VARCHAR(255))
IF in_first_name IS NOT NULL THEN
SELECT * FROM `contacts`
WHERE `owner_id` = in_owner_id AND `first_name` LIKE in_first_name;
END IF;
When I try and execute this on my MySQL server I get the following error:
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
I'd like to know what is causing this error and why so I can avoid it again.
Any help is appreciated!
Try adding "BEGIN", "END" and "DELIIMITER", like this:
DELIMITER $$
CREATE PROCEDURE `Search_contacts`(IN `in_owner_id` INT,
IN `in_first_name` VARCHAR(255))
BEGIN
IF in_first_name IS NOT NULL THEN
SELECT * FROM `contacts`
WHERE `owner_id` = in_owner_id AND `first_name` LIKE in_first_name;
END IF;
END $$
DELIMITER ;

Procedure breaking when trying to import into DB

I have the following procedure that when I manually import this is breaking for some unknown reason.
CREATE PROCEDURE `register_house`(
IN UID CHAR(17),
IN new_username VARCHAR(16),
IN new_signature CHAR(64),
IN email VARCHAR(128),
IN postcode VARCHAR(16),
IN customer_name VARCHAR(45),
IN phone_number VARCHAR(16)
)
BEGIN
UPDATE bb.checkin SET username = new_username, signature = new_signature WHERE _site = UID;
END
I'm getting the following error in Mysql Workbench -
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 11
Can someone suggest what the problem is?
UPDATE
As suggested i've amended to include the DELIMITER and now get the following error:
Error Code: 1728. Cannot load from mysql.proc. The table is probably corrupted
The tables all appear to look correct is the way to 'de-corrupt' them if they have corrupted somehow?
Based on https://stackoverflow.com/a/639356/2381157, try this
delimiter //
CREATE PROCEDURE `register_house2`(
IN UID CHAR(17),
IN new_username VARCHAR(16),
IN new_signature CHAR(64),
IN email VARCHAR(128),
IN postcode VARCHAR(16),
IN customer_name VARCHAR(45),
IN phone_number VARCHAR(16)
)
BEGIN
UPDATE bb.checkin SET username = new_username, signature = new_signature WHERE _site = UID;
END
//
delimiter ;
It worked for me in MySQL 5.5
This worked for me...
Added this to the start of the statement on line 1;
DELIMITER $$
then on the ver
END $$
Then finally to fix the 'corrupted' message I simply ran the following file:
C:\xampp\mysql\bin\mysql_upgrade.exe

Transaction with RollBack in mysql

By Creating this Strored Procedure i am getting error dont know how to fix it
Here is my sql
DROP PROCEDURE IF EXISTS Sp_Reservation;
DELIMITER $$
CREATE PROCEDURE Sp_Reservation
(
IN name VARCHAR(150),
IN email VARCHAR(100),
IN mobile VARCHAR(15),
IN cninc VARCHAR(15),
IN cityID INT(10),
IN checkin Date,
IN checkout Date,
IN noOfRooms INT(5),
IN RoomID INT(10),
IN RoomCategoryID INT(10),
IN noOfChilds INT(5),
IN noOfAdults INT(5),
IN message VARCHAR(500),
IN reservationStatus vARCHAR(10)
)
BEGIN
DECLARE CusID INT DEFAULT 1;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
END;
START TRANSACTION;
BEGIN
SET CusID =(
SELECT
IF(Max(customers.CustomerID) IS NULL,CUstID,customers.CustomerID) as
CutomID FROM customers);
INSERT INTO `customers` (customers.CustomerName,
customers.CustomerNID,
customers.CustomerEmail,
customers.CutomerMobile,
customers.CityID)
VALUES(name,
cnic,
email,
Mobile,
CityID);
INSERT INTO `roomreservation`(roomreservation.CustomerID,
roomreservation.RoomID,
roomreservation.CheckIn,
roomreservation.CheckOut,
roomreservation.NoOfAdults,
roomreservation.NoOfChildrens,
roomreservation.Message,
roomreservation.ReservationStatus)
SELECT 1,RoomID,checkin,checkout,noOfAdults,noOfChilds,message,reservationStatus
FROM rooms WHERE RoomID NOT IN
(
SELECT RoomID FROM roomreservation WHERE ReservationStatus = 'Reserved'
AnD roomreservation.CheckIn BETWEEN checkIn AND checkout
) ;
-- LIMIT (noOfRooms);
COMMIT;
END;
END$$
DELIMITER ;
ERROR:
SQL query:
DELIMITER ;
MySQL said:
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.
Note: I am using Phpmyadmin
Well, I don't see any issue with your procedure code except that you can try changing the statement END$$ to END $$. If still doesn't work then there is problem with your Phpmyadmin client and somehow it's not happy with the statement DELIMITER $$. Try using a different client (or) a different version of current client.

SQL Syntax error creating stored procedure

I am creating a small (for now) stored procedure to insert some data. When I create this I get an error.
DROP PROCEDURE IF EXISTS InsertTESTData;
delimiter $$
CREATE PROCEDURE InsertTESTData (
p_pink_no VARCHAR,
p_carrier VARCHAR,
p_thisno INT
)
BEGIN
INSERT INTO RefDB (pink_no,carrier,thisno)
VALUES
(p_pink_no,p_carrier,p_thisno);
END $$
delimiter ;
And the error I always get is a 1064 SQL error as follows.
Error : 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 'p_carrier VARCHAR,p_thisno IN) BEGIN INSERT INTO RefDB (pink_no,carrier,t' at line 2
I thought I had everything covered and am now pulling my hair out!
You need to define a length for your varchar parameters like this
p_pink_no VARCHAR(100)
and change
p_thisno IN
to
p_thisno INT
Complete example:
DROP PROCEDURE IF EXISTS InsertTESTData;
delimiter $$
CREATE PROCEDURE InsertTESTData (
p_pink_no VARCHAR(100),
p_carrier VARCHAR(100),
p_thisno INT)
BEGIN
INSERT INTO RefDB (pink_no,carrier,thisno)
VALUES (p_pink_no,p_carrier,p_thisno);
END $$
delimiter ;
you have to declare the size of the data type ,after thal i am sure your code will be execute successfully.
follow this
DROP PROCEDURE IF EXISTS InsertTESTData;
delimiter $$
CREATE PROCEDURE InsertTESTData (
p_pink_no VARCHAR(20),
p_carrier VARCHAR(200),
p_thisno number(23))
BEGIN
INSERT INTO RefDB (pink_no,carrier,thisno)
VALUES (p_pink_no,p_carrier,p_thisno);
END $$
delimiter ;