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 $$;
Related
I have below basic table:
create table hobbies (
id int primary key auto_increment,
name varchar(32) unique);
And I am trying to create a stored procedure to insert a new row in this table, like below:
delimiter //
create procedure add_hobby(hobby varchar(32))
begin
insert into hobbies(name) values(hobby);
end
delimiter //
call add_hobby('randomHobby');
When I call the procedure like above I am getting message "An unexpected error occured". I am running the queries on db-fiddle.com, MySQL ver 8.0. Could anyone offer some guidance if I've done something wrong or missed something? I can mention that procedure with select operation and no parameters works. Many thanks in advance.
You need to set your delimiter to begin with and then reset it at the end
for your parameters you need to set weather they are IN or out then the name and the type
delimiter //
create procedure add_hobby(IN hobby varchar(32))
begin
insert into hobbies(name) values(hobby);
end//
delimiter ;
While I created loads of procs in SQL server, I want to start using MariaDB and so tried creating the simple proc below in MySQL Workbench.
I keep on getting an error stating that there is a missing SELECT on the opening '(' after the table name:
DELIMITER $$
drop procedure if exists usp_AddSentEmail$$
CREATE PROCEDURE usp_AddSentEmail (in pSender varchar(36)
,in pTo varchar(1000)
,in pSubject varchar(100)
,in pBody varchar(10000)
,in pRecordDT datetime)
BEGIN
INSERT INTO Emails('To','Subject','Body','Sender','RecordDT','Sent','SentDT')
VALUES (pTo,pSubject,pBody,pSender,pRecordDT,1,pRecordDT);
END$$
DELIMITER ;
Maybe I am trying the wrong google search but that all comes up is delimiter errors.
remove the quotes from the column names in your insert query:
INSERT INTO Emails(To,Subject,Body,Sender,RecordDT,Sent,SentDT)
VALUES (pTo,pSubject,pBody,pSender,pRecordDT,1,pRecordDT);
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 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 ;
i have this table
create table eveniment( +
evenimentId bigint not null auto_increment primary key,
evenimentDenumire varchar(500),
adresaId int not null);
create table adresa(
adresaId bigint not null auto_increment primary key,
localitate varchar(500),
judet varchar(500),
codPostal varchar(50),
strada varchar(500),
nr varchar(50),
bl varchar(50),
ap varchar(5),
email varchar(500),
www varchar(500));
and this procedure
DELIMITER //
drop procedure IF EXISTS insertEveniment;
CREATE PROCEDURE insertEveniment()
BEGIN
DECLARE vAdresaEvenimentId int DEFAULT 0
insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www)
values('judet','localitate','cod postal','strada','numar','bloc','ap','email','www');
select last_insert_id() into vAdresaEvenimentId;
DECLARE vEvenimentId int DEFAULT 0
insert into eveniment(evenimentDenumire,adresaId) values('concurs informatic 1',vAdresaEvenimentId);
select last_insert_id() into vEvenimentId;
END //
DELIMITER ;
call insertEveniment();
and i get this error
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 'insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www) values(' at line 4
if i try the insert into adresa ... separately in mysql command i don't get error , what i do wrong?
Try like this (tried and it works for me):
drop procedure IF EXISTS insertEveniment;
DELIMITER //
CREATE PROCEDURE insertEveniment()
BEGIN
DECLARE vAdresaEvenimentId int DEFAULT 0;
DECLARE vEvenimentId int DEFAULT 0;
insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www)
values('judet','localitate','cod postal','strada','numar','bloc','ap','email','www');
select last_insert_id() into vAdresaEvenimentId;
insert into eveniment(evenimentDenumire,adresaId) values('concurs informatic 1',vAdresaEvenimentId);
select last_insert_id() into vEvenimentId;
END //
DELIMITER ;
call insertEveniment();
You are using drop procedure statement with wrong delimiter (you were setting delimiter to // but then you put ; at the end of your statement. Put your drop procedure statement before you set the delimiter, and it will work just fine. Also, put delimiters after DECLARE
DECLARE is a separate statement, thus it needs to be delimited with a ;, like INSERT or SELECT or any other statement.
Also, move the first DELIMITER statement to just before the beginning of the procedure declaration.
So,
DELIMITER //
drop procedure IF EXISTS insertEveniment;
DELIMITER //
CREATE PROCEDURE insertEveniment()
BEGIN
DECLARE vAdresaEvenimentId int DEFAULT 0 ;
insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www)
values('judet','localitate','cod postal','strada','numar','bloc','ap','email','www');
select last_insert_id() into vAdresaEvenimentId;
DECLARE vEvenimentId int DEFAULT 0 ;
insert into eveniment(evenimentDenumire,adresaId) values('concurs informatic 1',vAdresaEvenimentId);
select last_insert_id() into vEvenimentId;
END //
DELIMITER ;
call insertEveniment();