MySQL SP with transaction correct syntax with in parameters - mysql

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
$$

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 stored procedure syntax troubles

I'm working on this procedure but I'm not too familiar with writing a stored procedure in MySQL, when I try to create the stored procedure as shown here, it keeps telling me that a syntax is incorrect at line 9
DELIMITER//
CREATE PROCEDURE InsertUser (
IN new_USER_ID BIGINT,
IN new_USER_NAME VARCHAR(36),
IN new_ENCRYTED_PASSWORD VARCHAR(128),
IN new_ENABLED BIT,
IN select_Role_ID BIGINT)
BEGIN
DECLARE user_role_id BIGINT
SET user_role_id = (SELECT max(id) from USER_ROLE);
Insert into App_User(USER_ID, USER_NAME, ENCRYTED_PASSWORD, ENABLED)
values(new_USER_ID, new_USER_NAME, new_ENCRYTED_PASSWORD, new_ENABLED);
INSERT INTO USER_ROLE(ID, USER_ID, ROLE_ID)
values(user_role_id+1, new_USER_ID, select_Role_ID);
END;//
DELIMITER;
Can someone show me how the correct syntax should be?
Here's the correct way of using delimiter. See mysql documentation
DELIMITER //
CREATE PROCEDURE InsertUser (
IN new_USER_ID BIGINT,
IN new_USER_NAME VARCHAR(36),
IN new_ENCRYTED_PASSWORD VARCHAR(128),
IN new_ENABLED BIT,
IN select_Role_ID BIGINT)
BEGIN
Insert into App_User(USER_ID, USER_NAME, ENCRYTED_PASSWORD, ENABLED)
values(new_USER_ID, new_USER_NAME, new_ENCRYTED_PASSWORD, new_ENABLED);
INSERT INTO USER_ROLE(ID, USER_ID, ROLE_ID)
values(user_role_id+1, new_USER_ID, select_Role_ID);
END //
DELIMITER ;

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 ;

stored procedure and triggers in mysql

I'm trying to run a stored procedure that will insert data to the table. In in the table, there is a trigger after add that will update the sequence table(there is no sequence in MySQL DB)
This is my stored procedure:
DELIMITER $$
CREATE DEFINER=`dbName`#`%` PROCEDURE `insertGuide`(m_name varchar(45) ,m_last varchar(45) ,addres varchar(45) ,mphone int)
BEGIN
INSERT INTO `guides` (`id`, `name`, `lastName`, `address`, `phone`)
select max(id)+1, m_name, m_last, addres, mphone
from seq;
END
This is my trigger:
USE `dbName`;
DELIMITER $$
CREATE DEFINER=`dbName`#`%` TRIGGER
guidesUpdate
AFTER INSERT ON
guides
FOR EACH ROW
BEGIN
INSERT INTO seq VALUES ();
END
when I run
call insertGuide('a' ,'aa' ,'aaa' ,'12333')
it returns an error:
Error Code: 1442. Can't update table 'seq' in stored function/trigger
because it is already used by statement which invoked this stored
function/trigger.
Can I do something about it?
The best thing you can do is to eliminate this trigger entirely. If you want to have an auto-incrementing id on a table, then simply define it in the create table statement:
create table guides as (
id int not null auto_increment primary key,
. . .
);