MySQL Update Statement does not work in Stored Procedure - mysql

I have a simple sql update statement for MySQL. When I run it alone, it works fine.
UPDATE
temp_student
INNER JOIN student
ON temp_student.card = student.card
SET studentid = student.id;
But when I put it in a stored procedure, none of the rows in the table is updated. Anyone can provide a clue? Thanks.
DELIMITER $$
USE `eceintern2`$$
DROP PROCEDURE IF EXISTS `copy_from_temp_student`$$
CREATE DEFINER = `root` #`localhost` PROCEDURE `copy_from_temp_student` ()
BEGIN
UPDATE
temp_student
INNER JOIN student
ON temp_student.card = student.card
SET studentid = student.id;
END $$
DELIMITER ;

The syntax seems to be no problem.
DELIMITER $$
CREATE PROCEDURE `copy_from_temp_student`()
BEGIN
UPDATE `temp_student`
INNER JOIN `student` ON `temp_student`.`card` = `student`.`card`
SET `temp_student`.`studentid` = `student`.`id`;
END */$$
DELIMITER ;
SQL Fiddle demo

Related

Delete Stored Procedure not working in SQL

I'm trying to create a stored procedure for deleting a record in my table.
I have tried the following but it doesn't seem to work:
Delimiter //
CREATE PROCEDURE BIRD_STRIKE_INCIDENT_DELETE #row text
AS
BEGIN
DELETE FORM bird_strike.incidents WHERE row_names = #row
END
delimiter ;
Call BIRD_STRIKE_INCIDENT_DELETE('11')
Can someone provide pointers on what I might be doing wrong here?
Thanks!
Your code looks more like SQL Server than MySql
This should be so
Delimiter //
CREATE PROCEDURE BIRD_STRIKE_INCIDENT_DELETE (_row text)
BEGIN
DELETE FROM bird_strike.incidents WHERE row_names = _row;
END//
delimiter ;
You can consider this approach.
IF EXISTS(SELECT 1 FROM sys.procedures
WHERE Name = 'BIRD_STRIKE_INCIDENT_DELETE')
BEGIN
DROP PROCEDURE dbo.BIRD_STRIKE_INCIDENT_DELETE;
END;
else
BEGIN
CREATE PROCEDURE BIRD_STRIKE_INCIDENT_DELETE (_row text)
BEGIN
DELETE FROM bird_strike.incidents WHERE row_names = _row;
END
END;

Translate a Merge statement used in sql server in Mysql

Please support, I had a stored procedure create in sql server database using a Merge statement inside. I would like to use the same stored procedure in a Mysql database. Unfortunatly it seem the Merge funtion not work in MySql. Anybody can help me to do that ? Below my stored procedure
ALTER PROCEDURE [dbo].[ValiderFacturePharmacie]
#numdossierhospi VARCHAR(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
MERGE INTO pharmacie P
USING (SELECT NumDossHp, SUM(TotalProd) AS Total
FROM pharmacie WHERE NumDossHp = #numdossierhospi
GROUP BY NumDossHp) T
ON (P.NumDossHp = T.NumDossHp)
WHEN MATCHED THEN
UPDATE SET
P.TotalPharma = T.Total,
P.Etat ='VALIDE';
END
Thanks you for your answer ! I solved the issue by using this statement below
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `ValiderFacturePharmacie`(IN `numdossierhospi` VARCHAR(30))
NO SQL
UPDATE pharmacies
SET totalpharma = (select SUM(totalprod) from pharmacies where numdosshp = numdossierhospi),
etat = 'FACTURER'
WHERE numdosshp = numdossierhospi$$
DELIMITER ;
It is alomst the same
DELIMITER //
CREATE PROCEDURE ValiderFacturePharmacie (
_numdossierhospi VARCHAR(50))
BEGIN
UPDATE pharmacie P INNER JOIN
(SELECT NumDossHp, SUM(TotalProd) AS Total
FROM pharmacie WHERE NumDossHp = _numdossierhospi
GROUP BY NumDossHp) T
ON (P.NumDossHp = T.NumDossHp)
SET
P.TotalPharma = T.Total,
P.Etat ='VALIDE';
END //
DELIMITER ;

IF NOT EXIST and Insert mysql stored procedure

hey guys can someone point me whats wrong in my mysql code
im trying to create a stored procedure called in a trigger
were a user insert a new book in the books table, the bookqty table insert if callNumber does not exist and updates when exist, but in some point the insert query is not working
but the update query works fine
thank you in advance
use librarydb;
drop procedure if exists intoBooksQty;
delimiter $$
create procedure intoBooksQty(in newcallNumber varchar(10))
begin
if not exists (select * from books where callNumber = newcallNumber) then
insert into librarydb.bookqty(callNumber,bookQty,bookqtyOut) values (newcallNumber, 1,0);
else
update bookqty set bookQty = bookQty + 1 where callNumber = newCallNumber;
end if;
end$$
delimiter ;
One option is to make a UPSERT.
DELIMITER //
CREATE PROCEDURE `intoBooksQty`(`newcallNumber` VARCHAR(10))
BEGIN
INSERT INTO `bookqty` (`callNumber`, `bookQty`, `bookqtyOut`)
SELECT `newcallNumber`, 1, 0
FROM `books`
WHERE `callNumber` = `newcallNumber`
ON DUPLICATE KEY UPDATE `bookQty` = `bookQty` + 1;
END//
DELIMITER ;
SQL Fiddle demo

MYSQL Trigger Query

I am attempting to create a trigger to update quantities between 2 separate databases. This query runs successfully, but when I show triggers in mysql, it brings up an empty set. Any help would be much appreciated.
delimiter $$ CREATE TRIGGER `quantity_to_clb` AFTER UPDATE ON product
FOR EACH ROW BEGIN UPDATE cl_boutique.product AS clb
LEFT JOIN cherrylane.product AS cl
ON clb.model = cl.code SET clb.quantity = cl.available
WHERE clb.model = cl.code
END $$
delimiter ;
That's because your code has two synax errors:
delimiter $$ --delimiter statements need to be on separate lines
CREATE TRIGGER `quantity_to_clb` AFTER UPDATE ON product
FOR EACH ROW BEGIN
UPDATE cl_boutique.product AS clb
LEFT JOIN cherrylane.product AS cl
ON clb.model = cl.code SET clb.quantity = cl.available
WHERE clb.model = cl.code; -- ; was needed here
END $$
delimiter ;

mysql stored procedure updates all rows instead of 1

Here is the MySQL procedure:
CREATE DEFINER = `root`#`%` PROCEDURE `NewProc`(IN comp_id VARCHAR(40))
BEGIN
...
UPDATE tbl_complaint SET DIDM_Docket_No = '2013-12-12' WHERE Comp_ID = comp_id;
END;
This is how it looks when i call the procedure:
call gen_docketno('{74651651-9D76-C973-175A-97B9B78608A5}')
Is it because of the brackets and dash in value of the parameter that the procedure can't update properly? because when i run this in sql query it works but not when in stored procedure.
UPDATE tbl_complaint SET DIDM_Docket_No = '2013-12-12' WHERE Comp_ID = '{BF16E293-6CD2-8BC3-91B1-CF5AC70A090B}';
Can somebody please tell me how to fix this problem?
rename your parameter comp_id. it collides with your column name causing it to update all records,
CREATE PROCEDURE `NewProc`(IN _comp_id VARCHAR(40))
BEGIN
...
UPDATE tbl_complaint
SET DIDM_Docket_No = '2013-12-12'
WHERE Comp_ID = _comp_id;
END;