Drop procedure if exists in mysql - mysql

Hi i am trying to create a mysql script that I can run whenever I need to update my database. The script creates a table and then executes some stored procedures.
DELIMITER $$
CREATE TABLE IF NOT EXISTS tbl_name (
col1 bigint(20) NOT NULL AUTO_INCREMENT,
col2 varchar(255) NOT NULL,
col3 varchar(64) NOT NULL,
col4 datetime DEFAULT NULL,
PRIMARY KEY (`col1 `),
UNIQUE KEY col2 (`col2`)
) ENGINE=InnoDB AUTO_INCREMENT=572 DEFAULT CHARSET=utf8$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `myproc`(IN username
VARCHAR(255))
BEGIN
DECLARE var1 VARCHAR(64);
DECLARE expirationDate DATETIME;
SET var1 = 12345;
SET expirationDate = DATE_ADD(NOW(), INTERVAL 30 SECOND);
REPLACE INTO tbl_name (col2, col3, col4) VALUES (someval, var1, expirationDate);
END$$
DELIMITER ;
When I ran the script first time, it created the table and executed the stored procedure in MySQL Workbench. When I ran the same thing second time, I got the error 1304 procedure already exists.
I looked online here about dropping the procedure and then create again. But when I entered the below command before creating the procedure, i got an error on CREATE command with code 1064.
DROP PROCEDURE IF EXISTS myproc;
CREATE DEFINER=`root`#`localhost` PROCEDURE `myproc`(IN username
VARCHAR(255))
.
.
.
I am very new to mysql and not sure how to execute the procedure if it already exists.
Any help would be appreciated.

Since you changed to DELIMITER $$ you need to use that delimiter at the end of each statement until you change it back.
DROP PROCEDURE and CREATE PROCEDURE are separate statements, and each requires its own statement delimiter.
DROP PROCEDURE IF EXISTS myproc $$
Note the delimiter at the end of the line above.
CREATE DEFINER=`root`#`localhost` PROCEDURE `myproc`(IN username
VARCHAR(255))
.
.
.
END $$
And another delimiter at the end of the whole CREATE PROCEDURE statement.

Related

When I create a variable in create procedure on mySQL I get an error

//This is the code
create procedure st_insertRole(
#name varchar(30) //I get the error here
as
insert into roles values(#name)
);
here is the screenshot
My SQL does not allow session variables to be used for procedure parameters. You also have parentheses misplaced. The code in MySQL would look more like:
delimiter $$
create procedure st_insertRole (
in_name varchar(30)
)
begin
insert into roles (name)
values (in_name);
end; $$
The syntax is wrong
create procedure st_insertRole(
name_ varchar(30) )
insert into roles values(name_)
;

MySQL SP with transaction correct syntax with in parameters

To quite simply put how my SP looks it is basically like this atleast syntax wise
DELIMITER $$
DROP PROCEDURE IF EXISTS `info_insert_or_update` $$
CREATE PROCEDURE `info_insert_or_update` (
IN in_id bigint,
IN in_name varchar(150),
IN in_details varchar(150))
START TRANSACTION;
INSERT INTO infos (id, name)
VALUES (in_id, in_name)
ON DUPLICATE KEY UPDATE name = in_name;
INSERT INTO details (details_id, details)
VALUES(in_id,
in_details)
ON DUPLICATE KEY UPDATE details = in_details;
COMMIT;
END$$
DELIMITER ;
With this the problem is that it cant recognize the in_ variables and i understand that that is because i need an compound statement with BEGIN END around everything but where ever i seem to put it it is something wrong with the syntax. So what is the correct syntax when i got this type of SP with in parameters that then has an transaction? (want transaction as i will add rollback onto it as well)
You are missing datatype for the in_details-parameter and you are missing the starting BEGIN
DELIMITER $$
DROP PROCEDURE IF EXISTS `info_insert_or_update`
$$
CREATE PROCEDURE `info_insert_or_update` (
in_id bigint,
in_name varchar(150),
in_details varchar(150)
)
BEGIN
START TRANSACTION;
INSERT INTO infos (id, name)
VALUES (in_id, in_name)
ON DUPLICATE KEY UPDATE name = in_name;
INSERT INTO details (details_id, details)
VALUES(in_id, in_details)
ON DUPLICATE KEY UPDATE details = in_details;
COMMIT;
END
$$

MySQL stored procedure for table insert error

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 ;

MYSQL: procedure and trigger error

I'm rather new to MySQL and am encountering an error with my procedure, I want to be able to call the same thing for updates, inserts and deletes to a to my table so need a procedure to call.
I've looked at MySQL Fire Trigger for both Insert and Update and The MySQL "DELIMITER" keyword isn't working as well as some other but nothing helped. (the delimiter keyword also doesnt work producing a sepperate error but that doesnt help)
The error is this and I cant work out what it means:
"Error
SQL query:
-- trigger on work log change
DROP PROCEDURE IF EXISTS PROC_TRACK_CHANGES CREATE DEFINER = root#localhost PROCEDURE PROC_TRACK_CHANGES BEGIN INSERT INTO CHANGES( LoggedTimeStamp, EmployeeID, LogTimeStamp, ProposalID, WorkDone )
SELECT NOW( ) , EmployeeID, LogTimeStamp, ProposalID, WorkDone
FROM WORKLOG;
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 'CREATE DEFINER=root#localhost PROCEDURE PROC_TRACK_CHANGES
BEGIN
INSERT INTO ' at line 3"
The errorred code in question is this:
CREATE TABLE WORKLOG(
EmployeeID BIGINT UNSIGNED NOT NULL UNIQUE, -- FKEY PKEY
LogTimeStamp TIMESTAMP, -- PKEY
ProposalID BIGINT UNSIGNED NOT NULL UNIQUE, -- FKEY
WorkDone VARCHAR(200),
CONSTRAINT WORKLOG_FOREIGN_KEY_EMP FOREIGN KEY (EmployeeID) REFERENCES EMPLOYEES (EmployeeID),
CONSTRAINT WORKLOG_FOREIGN_KEY_PRO FOREIGN KEY (ProposalID) REFERENCES PROPOSALS (ProposalID),
CONSTRAINT WORKLOG_PROPOSALS_PRIMARY_KEY PRIMARY KEY (EmployeeID, LogTimeStamp)
);
CREATE TABLE CHANGES(
LoggedTimeStamp TIMESTAMP, -- PKEY
EmployeeID BIGINT UNSIGNED NOT NULL UNIQUE, -- FKEY
LogTimeStamp TIMESTAMP, -- fkey
ProposalID BIGINT UNSIGNED NOT NULL UNIQUE, -- FKEY
WorkDone VARCHAR(200),
CONSTRAINT CHANGES_FOREIGN_KEY_EMP FOREIGN KEY (EmployeeID) REFERENCES EMPLOYEES (EmployeeID),
CONSTRAINT CHANGES_FOREIGN_KEY_PRO FOREIGN KEY (ProposalID) REFERENCES PROPOSALS (ProposalID),
CONSTRAINT CHANGES_PRIMARY_KEY PRIMARY KEY (LoggedTimeStamp)
);
-- trigger on work log change
DROP PROCEDURE IF EXISTS PROC_TRACK_CHANGES
CREATE DEFINER=root#localhost PROCEDURE PROC_TRACK_CHANGES
BEGIN
INSERT INTO CHANGES (LoggedTimeStamp, EmployeeID, LogTimeStamp, ProposalID, WorkDone)
SELECT now(), EmployeeID, LogTimeStamp, ProposalID, WorkDone FROM WORKLOG;
END;
DROP TRIGGER IF EXISTS TR_WORKLOG_UPDATE
CREATE DEFINER=root#localhost TRIGGER TR_WORKLOG_UPDATE
BEFORE UPDATE ON 'WORKLOG'
FOR EACH ROW
BEGIN
CALL PROC_TRACK_CHANGES();
END;
DROP TRIGGER IF EXISTS TR_WORKLOG_INSERT
CREATE DEFINER=root#localhost TRIGGER TR_WORKLOG_INSERT BEFORE INSERT ON WORKLOG
FOR EACH ROW
BEGIN
CALL PROC_TRACK_CHANGES();
END;
DROP TRIGGER IF EXISTS TR_WORKLOG_DELETE
CREATE DEFINER=root#localhost TRIGGER TR_WORKLOG_DELETE BEFORE DELETE ON WORKLOG
FOR EACH ROW
BEGIN
CALL PROC_TRACK_CHANGES();
END;
It's probably something simple that i'm missing but the MYSQL debugger confuses me.
many thanks!
A procedure drop is a single statement and a procedure create too. Seperate them with the delimiter.
Another thing is that you need to define another delimiter than ;. Otherwise the DB will termiante every statement at the ; which makes some trigger and procedure definitions incomplete. So use for example
delimiter |
DROP PROCEDURE IF EXISTS PROC_TRACK_CHANGES
|
CREATE DEFINER=root#localhost PROCEDURE PROC_TRACK_CHANGES
BEGIN
INSERT INTO CHANGES (LoggedTimeStamp, EmployeeID, LogTimeStamp, ProposalID, WorkDone)
SELECT now(), EmployeeID, LogTimeStamp, ProposalID, WorkDone FROM WORKLOG;
END
|
Try this:
DROP PROCEDURE IF EXISTS PROC_TRACK_CHANGES;
Notice the semi-colon at the end.
You should specify a temporary delimiter other than semi-colon, and use that delimiter at the end of both your DROP PROCEDURE and CREATE PROCEDURE statements.
Also you need to add parentheses after the procedure name in the CREATE PROCEDURE statement even if it takes no arguments.
This should work for you:
DELIMITER $$
DROP PROCEDURE IF EXISTS PROC_TRACK_CHANGES $$
CREATE DEFINER=root#localhost PROCEDURE PROC_TRACK_CHANGES ()
BEGIN
INSERT INTO CHANGES (LoggedTimeStamp, EmployeeID, LogTimeStamp, ProposalID, WorkDone)
SELECT now(), EmployeeID, LogTimeStamp, ProposalID, WorkDone FROM WORKLOG;
END $$
DELIMITER ;

mysql stored procedure (with 2 insert) error

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();