I have this stored procedure :
DELIMITER $$
CREATE DEFINER=`old_dev_user`#`%` PROCEDURE `p_refresh_selling_table`(location_id VARCHAR(5))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
END;
SET sql_log_off = 1;
START TRANSACTION;
IF (location_id = '') THEN
DELETE FROM selling_table;
INSERT INTO selling_table
(column1,
column2,
column3,
..
..
.
)
(SELECT DISTINCT original_table.id,
..
..
..
..
);
ELSE
DELETE FROM selling_table where id=location_id;
INSERT INTO selling table
(column1,
column2,
column3,
..
..
.
)
(SELECT DISTINCT original_table.id,
..
..
..
..
);
END IF;
COMMIT;
END$$
DELIMITER ;
The issue I have is that the stored procedure executes without any errors but the results are not updated to the selling_table as expected:
mysql> CALL `site_database`.`p_refresh_selling_table`(81);
Query OK, 0 rows affected (0.01 sec)
I know the table should populate as expected because when I run the select part of the stored procedure ((SELECT DISTINCT original_table.id..) I get the output of results which then should go to the selling table.
I have also checked permissions and the user (although not the same as the definer) has ALL permissions to the database / schema which is used in the stored procedure.
Also I have duplicated this schema on a separate (local) environment and this stored procedure executes just fine, I can see the selling_table populated with the expected results.
Please point me to the correct direction as to why the selling_table is not being populated.
Thank You
I recommend that you install common_schema and use rdebug to debug your stored procedure:
http://code.openark.org/blog/mysql/taking-common_schemas-rdebug-to-a-test-drive
Related
I write a procedure for MariaDB. I need to verify correct code.
DELIMITER //
CREATE PROCEDURE verifyInsert (IN value TINYTEXT)
BEGIN
IF NOT EXISTS (SELECT `table1`.Column1 FROM `table1`
WHERE `column1` = valore)
then INSERT INTO `table1`(`column1`) VALUES (value)
END IF;
END;
// DELIMITER;
This procedure must be verify if an inserted value is present.
If yes i do not anything else it insert in my database.
Someone could verify my code please?
As said, make unique column. And you can use
INSERT IGNORE INTO
so you just "eat" the error and go on as planned.
the query works and updates as it supposed to, but when I tried to make it into a stored procedure, it fails with "error near WHERE". Can anyone see what is wrong please?
DELIMITER //
DROP procedure if exists update_trans_with_tags//
CREATE PROCEDURE update_trans_with_tags()
BEGIN
UPDATE transactions
SET trans_cat = CASE WHEN trans_desc LIKE '%abc%' THEN 1
WHEN trans_desc LIKE '%def%' THEN 2
WHEN trans_desc LIKE '%ghi%' THEN 4
ELSE trans_cat
END;
WHERE trans_cat IS NULL;
END//
DELIMITER ;
Take the ; off of the first END; ; still terminates statements within the stored procedure. If the hanging WHERE didn't prevent you from saving the procedure; the "first" statement would UPDATE all transactions.
I have an SQL script that I need to convert to a parameterized stored procedure. I've only written simple functions in the past, never a complex transactional query with parameters.
Any help is greatly appreciated - simplified queries below. This script could really be anything containing a transaction and some user inputs.
-- transaction ensures i can clean up a mess, if one happens
begin;
-- parameters for the script; currently set manually before execution
set #parent_id := 123;
set #identifier := 'someid';
-- insert some row with user-specified values
insert into users (field1, field2) values (#parent_id, #identifier);
-- get the new id
set #user_id := last_insert_id();
-- do another insert
insert into usersmeta (user_id, field1, field2) values (#user_id, 1, 2);
-- if no errors happened yet, commit transaction
commit;
-- "return value"; some select query (could be 1 or many rows)
select users.id userid, usersmeta metaid
from users
join usersmeta on usersmeta.user_id = users.id;
I started with but then I pretty much got stuck. I'm especially concerned with ensuring errors, in the event they occur, are somehow made visible to the calling code
delimiter ;;
CREATE PROCEDURE mytask(IN parent_id INT(11), IN identifier VARCHAR(200))
BEGIN
SET #query = ???
PREPARE q FROM #query;
EXECUTE q;
DEALLOCATE PREPARE q;
END;;
delimiter ;
It took a good amount of research, trial, and error, but I think I arrived at a pretty good solution.
DELIMITER //
CREATE PROCEDURE my_procedure (IN parent_id int, IN identifier varchar(255), OUT out_user_id int)
BEGIN
-- local variable, set later after user is created
DECLARE user_id int;
-- rollback transaction and bubble up errors if something bad happens
DECLARE exit handler FOR SQLEXCEPTION, SQLWARNING
BEGIN
ROLLBACK;
RESIGNAL;
END;
START TRANSACTION;
-- insert some row with user-specified values
INSERT INTO users (field1, field2) values (parent_id, identifier);
-- get the new id
SET user_id = last_insert_id();
-- do another insert
INSERT INTO usersmeta (user_id, field1, field2) values (user_id, 1, 2);
-- if no errors happened yet, commit transaction
COMMIT;
-- return
SELECT user_id INTO out_user_id;
END //
DELIMITER ;
I can use it like this
-- run the procedure
CALL my_procedure(123, 'some_id', #user_id);
-- get the "return" value
SELECT #user_id as user_id;
This is definitely the most complex stored procedure I've written. If anyone sees an area for improvement, I'd be happy to learn how I can make it better.
I have a stored procedure which refreshes table1 by selecting a bunch of fields from table2. The issue I have is that when I run the procedure it does not update table1.
Here is the procedure call statement:
CALL `dabaseName`.`p_refresh_table1`(<{loc_id VARCHAR(5)}>);
So if I call the procedure like so:
CALL `databaseName`.`p_refresh_table1`('12');
or like so:
CALL `databaseName`.`p_refresh_table1`(12);
It does NOT update results to table1
I have found the reason behind this so if I expand the SQL in this procedure and manually pass the id I see the following :
The SQL is:
DELETE FROM table1 where id=loc_id;
INSERT INTO selling table
(column1,
column2,
column3,
..
..
.
)
(SELECT DISTINCT table2.id,
..
..
..
..
);
If I run the sql with loc_id substituted with '12' (quoted) it works as expected
However If I run it with loc_id substituted with 12 (unquoted) it does not update table1.
So I need to know how I can pass '12' instead of 12 to the value of loc_id.
The procedure create statement starts like so:
DELIMITER $$
CREATE DEFINER=`userName`#`%` PROCEDURE `p_refresh_table1`(loc_id VARCHAR(5))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
END;
SET sql_log_off = 1;
START TRANSACTION;
Thanks
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 ;