while creating trigger in mysql i m getting error 1046. my query is:
CREATE TABLE test.Employee(
id int,
first_name VARCHAR(30),
last_name VARCHAR(15),
start_date DATE,
end_date DATE,
city VARCHAR(10),
description VARCHAR(15)
);
CREATE TABLE test.Employee_log(
id int,
first_name varchar(50),
last_name varchar(50),
start_date date,
end_date date,
city varchar(50),
description varchar(50),
Lasinserted Time
);
when i am executing below lines it is giving 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 'END$$
delimiter' at line 1
(16 ms taken)
delimiter $$
CREATE TRIGGER test.Employee_Trigger
AFTER insert ON test.employee
FOR EACH ROW
BEGIN
insert into test.employee_log values(new.id,new.first_name,
new.last_name,new.start_date,new.end_date,
new.city,new.description,curtime());
END$$
delimiter ;
if some one could please help it would be greately appreciated.
Thanks
Yugal
trigger are not routine nor stored procedure,
so there is not need for BEGIN...END, plus the table name is case sensitive
CREATE TRIGGER Employee_Trigger
AFTER insert ON Employee
FOR EACH ROW
INSERT INTO test.Employee_log values(new.id,new.first_name,
new.last_name,new.start_date,new.end_date,
new.city,new.description,curtime());
Related
I'm trying to create the table below but I get an error. I double checked and the data types are valid, and a table with this name doesn't currently exist in the database. I even closed and re-opened my terminal in case this was a glitch. But still no luck. Any idea what's wrong here?
create table order (order_no int, purch_amt decimal(6,2), order_date date, customer_id int, salesman_id int);
Error message I get:
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 'order (order_no int, purch_amt decimal(6,2), order_date date, customer_id int, s' at line 1
order is SQL keyword. I would suggest calling the table orders. I would suggest:
create table orders (
order_no int,
purch_amt decimal(6,2),
order_date date,
customer_id int,
salesperson_id int
);
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 ;
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.
I'm new to MYSQL and I'm trying to use a stored procedure to insert values into a table.can someone help me with this please
create procedure insertcust(in lname varchar(30), in fname varchar(30),dob1 date)
begin
insert into customer (lname,fname,dob)
values(lname,fname,dob1);//it is saying i have an incorrect syntax ere
end
If you run your procedure directly in phpmyadmin then you should write something like below
CREATE PROCEDURE `insertcust`(IN `lname ` VARCHAR(30), IN `fname` VARCHAR(30), IN `dob1` DATE)
NOT DETERMINISTIC NO SQL SQL SECURITY
DEFINER
insert into customer (lname,fname,dob)values(lname,fname,dob1)
I hope this will help you.
I am new to MySQL. I have two tables posts and notification. I am trying to create a trigger for every new row added in posts, I would to add that new row to notification_id.
its working properly on mysql but in phpmyadmin trigger won't execute , it gives 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 '' at line 11
Here how my tables look like:
posts table
create table posts
(
id int,
topic_id tinyint,
post_creator int,
post_date datetime,
post_content text
);
notification table
create table notification
(
not_id int ,
top_id tinyint,
p_creator int,
p_date datetime,
p_content text
);
notification_id trigger
delimiter $$;
create trigger notification_id before Insert on posts
for each row
begin
declare id int;
declare topic_id int;
declare post_creator int;
declare post_date datetime;
declare post_content text;
insert into notification(not_id,top_id,p_creator,p_date,p_content) values(new.id,new.topic_id,new.post_creator,new.post_date,new.post_content);
end$$;
create the tables first normally
Change the default delimiter in phpmyadmin interface [it is just under the textbox where you enter your query]
Run the create trigger script separately, without the lines of changing delimiter
So, your trigger's code should not have delimiter $$;