Getting the warning 0 row(s) affected, 1 warning(s):
1072 Key column 'name' doesn't exist in table And I do not know what it means. Does anybody have an explaination?
The table/SP is as follows
CREATE TABLE IF NOT EXISTS `sectors`
(
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
`sector` VARCHAR(25) NOT NULL ,
--
PRIMARY KEY (`id`) ,
UNIQUE INDEX `sector_idx` USING BTREE (`sector` ASC)
);
DELIMITER $$
CREATE PROCEDURE `AddSector` (IN sector VARCHAR(25),
OUT result BOOLEAN)
MODIFIES SQL DATA
BEGIN
DECLARE CONTINUE HANDLER FOR SQLWARNING, SQLEXCEPTION SET result = FALSE;
SET result = TRUE;
--
INSERT INTO `sectors` (`sector`) VALUES (sector);
COMMIT;
END $$
Change the procedure to:
DELIMITER $$
CREATE PROCEDURE `AddSector` (IN pSector VARCHAR(25), <<-- fix nameclash here
OUT result BOOLEAN)
MODIFIES SQL DATA
BEGIN
DECLARE CONTINUE HANDLER FOR SQLWARNING, SQLEXCEPTION SET result = FALSE;
SET result = TRUE;
INSERT INTO `sectors` (`sector`) VALUES (pSector); <<-- and here
-- COMMIT; <<-- You don't need a commit.
END $$
All stuff inside a stored proc is already run inside an implicit transaction, so the commit is not needed, and may actually be an error (not sure).
DELIMITER ;
Related
I have created a student table.
CREATE TABLE `student` (
`id` int DEFAULT NULL,
`name` varchar(5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
I have created a stored procedure inserting the data.
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_studentinsert1`(in_id int, in_name varchar(5),inn_id int, inn_name varchar(10))
BEGIN
DECLARE `_rollback` BOOL DEFAULT 0;
DECLARE exit HANDLER FOR SQLEXCEPTION
begin
ROLLBACK;
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'SQLError occured. Triggered ROLLBACK';
select 'err' as message;
-- ERROR
end;
start transaction;
insert into student(id,name) values(in_id,in_name);
INSERT INTO student(id,name) values(inn_id,inn_name);
commit;
select * from student;
END$$
DELIMITER ;
. I have written two insert queries inside of stored procedure. I have given first two input parameters valid data, For 2nd record , I have given invalid data, once run the sp, first record inserted & 2nd record exception happen but once exception happen , 1st record rollback not happen.
Please help me.
I keep getting a syntax error at line 9
CREATE PROCEDURE `ItemsAddOrEdit`(
_itm_id INT,
_itm_name VARCHAR(255),
_itm_price FLOAT(8,2)
)
BEGIN
IF _itm_id = 0 THEN
INSERT INTO items (itm_name, itm_price)
VALUES (_itm_name, _itm_price);
ELSE
UPDATE items
SET
itm_name = _itm_name,
itm_price = _itm_price
WHERE itm_id = _itm_id;
END IF;
END
Are the variables the problem? I've checked the table to see if I messed the names up, but it all seems fine to me.
Here's the table code
CREATE TABLE `items` (
`itm_id` INT(255) NOT NULL AUTO_INCREMENT,
`itm_name` VARCHAR(255) NOT NULL,
`itm_price` FLOAT(8,2) NOT NULL,
PRIMARY KEY (`itm_id`),
UNIQUE INDEX `itm_name` (`itm_name`)
)
You need to redefine Delimiter to something else, for eg: $$. This allows parser to ignore ; (hence do not execute statement on reaching ;).
Also, as a good practice, always use DROP PROCEDURE IF EXISTS, to avoid failing out in case procedure with same name already exists.
At the end, redefine the Delimiter back to ;
Try the following:
DELIMITER $$
DROP PROCEDURE IF EXISTS `ItemsAddOrEdit` $$
CREATE PROCEDURE `ItemsAddOrEdit`(
_itm_id INT,
_itm_name VARCHAR(255),
_itm_price FLOAT(8,2)
)
BEGIN
IF _itm_id = 0 THEN
INSERT INTO items (itm_name, itm_price)
VALUES (_itm_name, _itm_price);
ELSE
UPDATE items
SET
itm_name = _itm_name,
itm_price = _itm_price
WHERE itm_id = _itm_id;
END IF;
END $$
DELIMITER ;
I want to update two tables at the same time in my database. One table is for groups, and the other table is for members of groups:
CREATE TABLE IF NOT EXISTS groups (
group_id INTEGER UNSIGNED AUTO_INCREMENT,
group_name VARCHAR(150) NOT NULL DEFAULT '',
group_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (group_id)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8;
CREATE TABLE IF NOT EXISTS group_members (
group_mem_user_id INTEGER UNSIGNED NOT NULL,
group_mem_group_id INTEGER UNSIGNED NOT NULL,
group_mem_role TINYINT DEFAULT 1,
group_mem_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT group_mem_pk PRIMARY KEY (group_mem_user_id, group_mem_group_id),
FOREIGN KEY (group_mem_user_id) REFERENCES user (user_id),
FOREIGN KEY (group_mem_group_id) REFERENCES groups (group_id)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8;
I want to use a stored procedure to create an entry in group and create an entry in group_members with the id that was just created for the group.
I know how to do this on the server (I have a java server and I'm using Spring's JdbcTemplate to make calls to the database) but I thought it would be better and more efficient to do this in a stored procedure.
The two individual queries are (im using prepared statements):
INSERT INTO groups (group_name) VALUES (?)
and
INSERT INTO group_members (group_mem_user_id, group_mem_group_id, group_mem_role) VALUES (?,?,?)
But I'm not sure how to merge these into one stored procedure.
DELIMITER //
DROP PROCEDURE IF EXISTS create_group //
CREATE PROCEDURE create_group(
#in/out here
)
BEGIN
#no idea
END //
DELIMITER ;
Ideally I would like it to return some value describing whether the operation was sucessful or not.
I use the following procedure:
DELIMITER //
DROP PROCEDURE IF EXISTS create_group //
CREATE PROCEDURE create_group(
IN create_group_group_name VARCHAR(150),
IN create_group_user_id INT
)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING
BEGIN
ROLLBACK;
END;
START TRANSACTION;
INSERT INTO groups (group_name) VALUES (create_group_group_name);
INSERT INTO group_members (group_mem_user_id, group_mem_group_id, group_mem_role) VALUES (create_group_user_id, LAST_INSERT_ID(), 2);
COMMIT;
END //
DELIMITER ;
In my server I use it like:
JdbcTemplate jt = new JdbcTemplate(DB.getDataSource(DB_USER));
int i = jt.update("CALL create_group (?,?)", new Object[] {groupName, userId});
if (i != 1)
throw new SQLException("Error creating group with name=" + groupName + " for userid=" + userId);
i == 1 if everything went well. Groups will never be created if the user is not added as a member (fixing the problem with my first iteration below).
OLD
(non-transactional, causes a problem if the second insert fails then the group is created with no members, it might work in some cases which is why I leave it here but it doesn't work for me)
The following procedure works. It does not return anything and I am just using the fact that the procedure completes without error to assume that it was all ok.
DELIMITER //
DROP PROCEDURE IF EXISTS create_group //
CREATE PROCEDURE create_group(
IN create_group_group_name VARCHAR(150),
IN create_group_user_id INT
)
BEGIN
INSERT INTO groups (group_name) VALUES (create_group_group_name);
INSERT INTO group_members (group_mem_user_id, group_mem_group_id, group_mem_role) VALUES (create_group_user_id, LAST_INSERT_ID(), 2);
END //
DELIMITER ;
Apparently I cannot combine the GET DIAGNOSTICS and ROLLBACK statements inside a stored procedure: besides catching the error, I want to be able to reverse all the previous processed data, not just to stop the execution. Please find bellow my script:
Create the log table:
CREATE TABLE tbl_logs (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`txt` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id`));
Create the stored procedure:
DELIMITER $$
CREATE PROCEDURE `test`()
BEGIN
DECLARE state CHAR(5) DEFAULT ’00000′;
DECLARE msg TEXT;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
begin
rollback;
GET DIAGNOSTICS CONDITION 1 state = RETURNED_SQLSTATE, msg = MESSAGE_TEXT;
insert into tbl_logs (txt) select concat(‘Error ‘,state,’: ‘,msg);
end;
start transaction;
insert into tbl_logs (txt) select state;
– drop table no_such_table;
insert into tbl_logs (txt) select ‘commit’;
commit;
END
call the procedure:
call test();
select * from tbl_logs;
=> check the table: 2 rows
00000
commit
Alter the procedure:
Take the comment out, making the drop table visible.
call the procedure:
call test();
select * from tbl_logs;
=> check the table: 2 new rows instead of just one, the last
00000
Error 42S02: Unknown table ‘no_such_table’
=> the handler catches the error and stops the execution, is just that doesn’t consider the rollback (no matter what/where other DML are previously made, they are committed)…
What am I doing wrong?
As documented under DROP TABLE Syntax:
Note
DROP TABLE automatically commits the current active transaction, unless you use the TEMPORARY keyword.
delimeter //
DROP function IF EXISTS get_seq_next//
create function get_seq_next(
IN sequence_ref_ varchar(30)
) returns int(11) unsigned
BEGIN
DECLARE seq_val_ int(11) unsigned;
LOCK TABLE ga_seq_tab WRITE;
select sequence_no into seq_val_ from ga_seq_tab where sequence_ref=sequence_ref_;
if not seq_val_ is null then
update ga_seq_tab set sequence_no=sequence_no+1 where sequence_ref=sequence_ref_;
end if
UNLOCK TABLES;
return seq_val;
END //
DELIMETER ;
I'm trying to create a function but it keeps saying I have syntax errors and I am not sure what is wrong with it
Try removing the IN reserved word in the parameters list.