How can I pass quoted value to a stored procedure - mysql

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

Related

I can't update my table with trigger in mysql

I'm trying to fill the empty field in the table after I update that table but I am get this error :
#1442 - Can't update table 'services_offer' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
How can I fix it?
my trigger's query:
DELIMITER $$
Create trigger OfferSum
after UPDATE
on Services_Offer
FOR EACH ROW
BEGIN
UPDATE Services_Offer
SET NEW.final_price =
(SELECT service_price from Services_Level
where Services_Level.level_code= Services_Offer.level_code)*Services_Offer.service_size;
END$$
I think you want a before update trigger:
DELIMITER $$
CREATE TRIGGER OfferSum BEFORE UPDATE ON Services_Offer
FOR EACH ROW
BEGIN
SELECT NEW.final_price := sl.service_price * NEW.service_size
FROM Services_Level sl
WHERE sl.level_code = NEW.level_code;
END$$
Why do you need a trigger for this? It looks like the query above will result in and endless loop. Every time you update a row, it calls an update trigger which actually updates the table and thus triggers the trigger again and again and again until something crashes.
Instead, why don't you calculate and update the column withing the initial update. The query would look like this:
UPDATE Services_Offer
SET
column1 = value1,
column2 = value2,
final_price =
(
SELECT service_price
FROM Services_Level
WHERE Services_Level.level_code = Services_Offer.level_code
) * service_size
WHERE id = XXX
where column1 = value1, column2 = value2 comes from your initial update query.

stored procedure executing but not updating the table as expected

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

How can I ignore the resultset of a stored procedure in a trigger?

My question is basically this one but for MySQL instead of SQL Server.
I have a stored procedure which returns a result set.
CREATE STORED PROCEDURE CreateFoo (input_id INT)
DECLARE result_id INT;
INSERT INTO FooTable (blah blah) VALUES (blah blah);
SELECT LAST_INSERT_ID() INTO result_id;
INSERT INTO OtherTable (x, y, blah) VALUES (input_id, result_id, blah);
SELECT result_id;
END
I have a trigger on another table
CREATE TRIGGER MyTrigger AFTER INSERT ON Bar
FOR EACH ROW
BEGIN
CALL CreateFoo (NEW.a);
CALL CreateFoo (NEW.b);
END
But now when I insert into Bar I get
ERROR 1415 (0A000) at line 5365: Not allowed to return a result set from a trigger
I understand why this is happening, now I need to quietly ignore the resultsets from CreateFoo.
In SQL Server the answer is to create a temporary table and INSERT INTO Temp CALL CreateFoo but apparently you can't do this in MySql.
Is there a way to drop the resultset from the trigger in MySql?
The only way I can think of is using OUT parameters (and this is actually the "clean" way of returning a result from a procedure, when it's just a single row).
Rewrite your procedure like this:
CREATE STORED PROCEDURE CreateFoo (IN input_id INT, OUT result INT)
INSERT INTO FooTable (blah blah) VALUES (blah blah);
SET result = LAST_INSERT_ID();
END
or
CREATE STORED PROCEDURE CreateFoo (IN input_id INT, OUT result INT)
INSERT INTO FooTable (blah blah) VALUES (blah blah);
SELECT LAST_INSERT_ID() INTO result;
END
You'd execute the procedure like this then:
CALL CreateFoo(5, #my_result);
When you need the last insert id, you can use it with
SELECT #my_result;
in the same session.
In your trigger of course you simply ignore the variable.
CREATE TRIGGER MyTrigger AFTER INSERT ON Bar
FOR EACH ROW
BEGIN
CALL CreateFoo (NEW.a, #variable_you_donT_care_about);
CALL CreateFoo (NEW.b, #variable_you_donT_care_about);
END
Read more about IN, OUT and INOUT parameters here.

Retrieve insert id in stored procedure with mysql

I have stored procedure for insert booking record with details like booking_id, name, r_id, t_id etc. Now I want to know how to retrieve insert id of this record.
Stored procedure allow LAST_INSERT_ID only.
Here MYSQL_INSERT_ID() not working.
But here at a time two users perform booking then last record is different.
Please help me how to get insert record id for the booking through Stored Procedures in my sql ..
You can capture the last_insert_id() value in different ways.
Define an 'OUT' parameter in procedure signature, with proper data type.
Now, after executing INSERT INTO..., execute set out_param := last_inser_id();
Now you can read value of 'OUT' parameter from the calling program or sql code body.
Example 1:
delimiter //
create procedure sp_so_q24075108( IN param1 int, ..., OUT param_auto_id int )
begin
insert
into table_name( col2, col3, ... )
values ( param1, param2, ... );
set param_auto_id := last_insert_id();
end;
//
delimiter ;
call sp_so_q24075108( 6, ..., #last_generated_id );
select #last_generated_id;
OR
Capture 'LAST_INSERT_ID()' into user variable after executing 'INSERT' statement.
Now you can read value of 'OUT' parameter from the calling program or sql code body.
Example 2:
delimiter //
create procedure sp_so_q24075108( IN param1 int, ... )
begin
insert
into table_name( col2, col3, ... )
values ( param1, param2, ... );
set #auto_id := last_insert_id();
end;
//
delimiter ;
call sp_so_q24075108( 6, ... );
select #auto_id;
Reference:
CREATE PROCEDURE Syntax

while loop terminating after fetching some data

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 ;