Given 2 simple procedures
DELIMITER $$
CREATE PROCEDURE simpleproc()
BEGIN
UPDATE mytable SET name = 'bbb' WHERE id = 1;
DO SLEEP(15);
END
$$ DELIMITER ;
DELIMITER $$
CREATE PROCEDURE simpleproc2()
BEGIN
UPDATE mytable SET name = 'jjj' WHERE id = 1;
END
$$ DELIMITER ;
Then in one connection (in mysql workbench) I run first procedure:
call simpleproc();
and in second connection, run second procedure
call simpleproc2();
What surprised me, is that second transaction no waits for first and executes immediately.
What I missed? why first transaction no locks row id=1?
Table mytable have InnoDB engine
Related
I try to call 1 stored procedure and do 2 things:
CALL PING(1)
update timestamp to row
and also
get back value from column
DELIMITER //
CREATE PROCEDURE ping (IN `P_in` INT)
BEGIN
UPDATE V_column SET my_timestamp=NOW() WHERE id=P_in;
SELECT updateFiles FROM V_column WHERE id=P_in;
END
I have this database structure :
Designer
I'm trying to implement this trigger so when a thread has reply, the number_replies goes up on every insertion of a thread if the parentId is not NULL
DELIMITER $$
CREATE TRIGGER `reply_count` AFTER INSERT ON `threads` FOR EACH ROW BEGIN
IF NEW.parentId != NULL THEN
UPDATE
threads
SET
threads.number_replies = threads.number_replies + 1
WHERE
threads.id = NEW.parentId;
END IF ;
END $$ DELIMITER ;
I don't get any errors but it is not working. Thoughts?
I am trying to run a query which I first typed out like this:
DELIMITER $$
DROP PROCEDURE IF EXISTS development $$
CREATE PROCEDURE development()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE i <= 500 DO
IF NOT EXISTS ((SELECT * FROM customers WHERE customerID = 500)) THEN INSERT INTO customers (firstName) VALUES (''); END IF;
SET i = i + 1;
END WHILE;
END $$
CALL development() $$
DROP PROCEDURE IF EXISTS development $$
DELIMITER ;
but then "compressed" into this:
DELIMITER $$ DROP PROCEDURE IF EXISTS development $$ CREATE PROCEDURE development() BEGIN DECLARE i INT DEFAULT 1; WHILE i <= 500 DO IF NOT EXISTS ((SELECT * FROM customers WHERE customerID = 500)) THEN INSERT INTO customers (firstName) VALUES (''); END IF; SET i = i + 1; END WHILE; END $$ CALL development() $$ DROP PROCEDURE IF EXISTS development $$ DELIMITER ;
on one line. The problem is that the first (multiline) code works, and does what it's supposed to do, while the other (single line) version, doesn't. It doesn't fail or throw errors, it just doesn't insert the rows like the multiline version. Why is this? More importantly, how can I make the single line version work?
Thanks in advance!
please try it this way:
DELIMITER $$
DROP PROCEDURE IF EXISTS development $$ CREATE PROCEDURE development() BEGIN DECLARE i INT DEFAULT 1; WHILE i <= 500 DO IF NOT EXISTS ((SELECT * FROM customers WHERE customerID = 500)) THEN INSERT INTO customers (firstName) VALUES (''); END IF; SET i = i + 1; END WHILE; END $$ CALL development() $$ DROP PROCEDURE IF EXISTS development $$
DELIMITER ;
The last line break might not be neccessary.
But I am fairly confident that, when inlining the whole code you end up setting a delimiter much longe than "$$"
I just executed the completely inlined example on a random sql server of mine and always received a positive result, even though I have not set up the related db structure.
with line breaks there are complaints about missing tables (as would be expected)
Regards
Stefan
You cannot run any SQL on the same line as delimiter. delimiter is a built in mysql client command that sets the statement seperator, not an sql interpreter. It takes one argument, read up to the first space or newline. Everything else is ignored.
delimiter $$ select now() $$
No output, because everything after $$ is thrown away.
Or illustrated by bad syntax
delimiter "$$" select; nothing order by from nowhere group , oh forget it $$
I want to write stored proc with transaction. Suppose, proc1 is calling another proc(proc2) in transaction block. proc2 also contains rollback/commit. If rollback happens in proc1, does rollback happen in proc2 also ??
I have tried following example but it didn't work.
Here is the following code
test1.sql
DELIMITER $$
DROP PROCEDURE IF EXISTS `debug`.`test1`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `test1`()
begin
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET #errorOccurred :=2;
set #errorOccurred :=1;
START TRANSACTION;
insert into Table1 values("table1_1");
insert into debug values("insertStatement_1");
insert into Table1 values(1,2);
call test2();
if (#errorOccurred=2) then
ROLLBACK;
else
COMMIT;
end if;
end$$
DELIMITER ;
=========================================
test2.sql
DELIMITER $$
DROP PROCEDURE IF EXISTS `debug`.`test2`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `test2`()
begin
START TRANSACTION;
create table Table2 (msg varchar(255)) ENGINE=InnoDB;
insert into Table2 values("table2_value");
COMMIT;
end$$
DELIMITER ;
Could you please help me to solve my problem.
Thank you in advance.
Here is something interesting: Once you run START TRANSACTION, any open transaction will implicitly commit. I wrote about this in the DBA StackExchange: March 15, 2013 : MySQL backup InnoDB.
SUGGESTION : I would remove START TRANSACTION; and COMMIT; from test2.sql.
Give it a Try !!!
UPDATE 2013-10-28 09:16 EDT
If you can calling a transaction within a transaction, you cannot do START TRANSACTION a second time. If you want to rollback from test2 and keep test1 in a transaction from the point of calling test2, you should look into using SAVEPOINT and ROLLBACK TO SAVEPOINT in the MySQL Docuementation.
Basically, you would create the SAVEPOINT in test1 just before calling test2. You would rollback to the SAVEPOINT if test2 fails in anyway.
I have written a procedure that creates a temporary table and executes a query by fetching the rows from the temporary table.I have around 13486 rows in the temporary table.But when i am calling the procedure i observed that the procedure is getting terminated after fetching 107 rows from the temporary table.Moreover i also observed that this value is not constant..Sometimes it is 107 the other time it is 114 and some other time it is just 100.Why this happens?Please need help?Somebody please..Here is my procedure.And i came to know that while loop will terminate for >1000 iterations.Please suggest me a method to overcome this.
DELIMITER $$
DROP PROCEDURE IF EXISTS `lookup`.`test` $$
CREATE PROCEDURE `lookup`.`test` ()
BEGIN
CREATE TEMPORARY TABLE lookup.airportname(id int AUTO_INCREMENT,PRIMARY KEY(id))
AS (select distinct airport_id from lookup.airport);
SET #num=0;
SET #arpt=NULL;
SELECT count(*) INTO #num FROM airportname;
SET #i=0;
while #i<#num do
SELECT airport_id INTO #arpt FROM airportname WHERE id=#i;
select #arpt,#i;
set #i=#i+1;
end while;
END $$
DELIMITER ;
I am using mysql query browser.Thank you.
If you want to insert value into id Then it should not be auto_increment. - Remove auto_increment from table definition, it should work
delimiter |
create procedure employee_select()
Begin
Declare empno,done int(9);
Declare emp_select Cursor for select emp_no from gross_salary ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
Open emp_select; // cursor opening
read_loop: loop // start looping all the datas one by one
fetch emp_select into empno; // fetching the select value into variable empno
//note :variable name should not be same as columne name in select statement"
IF done THEN
LEAVE read_loop; // if no more rows, this makes it to leave the loop"
END IF;
//Enter the code you want do for each row
end loop;
close emp_select;
End |
delimiter ;