Translate a Merge statement used in sql server in Mysql - 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 ;

Related

How to Trigger in MySql

I want to create a Trigger in MySql I did it with SQL Server at school in my class, but for my project, my web hoster only give me MySql DataBase and if I want a SQL Server DB I need to buy a VPS and I can't afford it so i'll do with MySql, I read about MySql Trigger and I saw that they are really more hard to do than SQL Server Trigger.
First here is every article I read about trigger in MySql (To be sure to not be placed as a duplica):
https://www.mysqltutorial.org/mysql-triggers.aspx/
https://www.mysqltutorial.org/create-the-first-trigger-in-mysql.aspx/
https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html
https://stackoverflow.com/questions/38745441/how-to-write-trigger-with-multiple-condition-in-mysql
https://stackoverflow.com/questions/33775826/how-to-declare-variables-in-trigger-with-mysql
And here is the SQL Server code I made:
CREATE TRIGGER file_verified_if_admin ON tblOpenSource
AFTER INSERT, UPDATE
AS
BEGIN
DECLARE #source_id INT;
DECLARE #verified TEXT;
DECLARE #client_id INT;
DECLARE c_info CURSOR FOR
SELECT source_id, verified, client_id FROM inserted;
OPEN c_info;
FETCH c_info INTO #source_id,#verified,#client_id;
WHILE (##FETCH_STATUS=0)
BEGIN
IF ((SELECT tblClients.grade FROM tblClients JOIN tblOpenSource ON tblOpenSource.client_id = tblClients.client_id;) == 'admin')
BEGIN
UPDATE tblOpenSource SET verified = 'verified' WHERE source_id = #source_id;
END;
END;
CLOSE c_info;
DEALLOCATE c_info;
END;
Here is what I made with MySql and I still have errors, can someone help me ?
CREATE TRIGGER file_verified_if_admin
AFTER INSERT, UPDATE ON tblOpenSource
FOR EACH ROW
BEGIN
DECLARE client_grade text;
SET #client_grade = (SELECT tblClients.grade FROM tblClients JOIN tblOpenSource ON tblClients.client_id = tblOpenSource.client_id);
IF (#client_grade = 'admin')
UPDATE tblOpenSource SET verified = 'verified' WHERE source_id = NEW.source_id;
END;
It says I have an error near my DELCARE, but I don't understand why, because I heard we don't need an "#" in the declaration.
What it is supposed to do, is to look if the file as been publied by an admin and if yes, it should update the verified column and change the text for verified
Mysql desn_'t allow multiple trigger in one goal you have to define then all.
Maysql needs DELIMITERto know where the trigger starts and ends
Last the IÌF CLause`is in mysql different.
This is the correct syntax, you can change it further by using SELECT INTO
UPDATE #Akina poited out,
that your query will not run, you can't change a row that has triggert the Trigger, mysql will not not allow that, so for your insert trigger, has to be change accordingly.
For the UPDATE Trigger goes teh saem.
DELIMITER //
CREATE TRIGGER file_verified_if_admin
BEFORE INSERT ON tblOpenSource
FOR EACH ROW
BEGIN
SET #client_grade = (SELECT tblClients.grade FROM tblClients JOIN tblOpenSource ON tblClients.client_id = tblOpenSource.client_id);
IF (#client_grade = 'admin') then
SET NEW.verified = 'verified';
END IF;
END//
DELIMITER ;

MySQL Update Statement does not work in Stored Procedure

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

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;

Mysql procedure sum

I cannot remember how to do a basic SUM in a SQL proceedure, can anybody remind me quickly please?
drop procedure if exists pCaCalculateCcyFluc;
delimiter //
create procedure pCaCalculateCcyFluc(
IN pPrice DECIMAL(10,6),
IN pPricePrev DECIMAL(10,6),
OUT pCcyFluc DECIMAL(10,6)
)
MODIFIES SQL DATA
COMMENT 'calculate fluctuation'
begin
pCcyFluc = (pPricePrev - pPrice)/pPrice;
--
end;
//
delimiter ;
To use the SUM Function:
SELECT SUM(TableName.ColumnName)
FROM TableName
JOIN TableName ON TableName.ColumnName = TableName.ColumnName
GROUP BY TableName.ColumnName;
Check out these examples.

MySQL Stored Procedures

I'm coming from a MS SQL Server background.
Working on a new project using MySQL with NaviCat 8 Admin tools.
Ok, here's the question.
Normally when working in MS land if I want to update some data I use a stored procedure to do this:
Drop Procedure spNew
Create Procedure spNew (#P_Param)
UPDATE Table
SET Field = 'some value'
WHERE ID = #P_Param
I am trying to do this same logic from within NaviCat.
I defined the Parameter, (IN '#P_Param' int)
In the Definition i placed:
BEGIN
UPDATE Table
SET Field = 'some value'
WHERE ID = #P_Param
END;
When I try and save the stored procedure, i'm getting this error:
"1064 - You have an error in your SQL syntax, blah, blah, blah"
Can anyone at least point me in the right direction?
Thanks.
CREATE PROCEDURE spNew(P_Param INT)
BEGIN
UPDATE Table
SET Field = 'some value'
WHERE ID = P_Param;
END;
Note that MySQL syntax and overall ideology are very different from those of SQL Server.
You may also need to set delimiter:
DELIMITER $$
CREATE PROCEDURE spNew(P_Param INT)
BEGIN
UPDATE Table
SET Field = 'some value'
WHERE ID = P_Param;
END;
$$
DELIMITER ;
BTW, I'm assuming you don't actually call your table "Table", since it's a reserved word.
If you do, you need to enclose it into backticks like this:
DELIMITER $$
CREATE PROCEDURE spNew(P_Param INT)
BEGIN
UPDATE `Table`
SET `Field` = 'some value'
WHERE `ID` = P_Param;
END;
$$
DELIMITER ;
Parameters to MySQL stored procedures aren't prefixed with # or quoted in either the declaration or when used. Local variables are prefixed with #, however.
Try:
DROP PROCEDURE IF EXISTS spNew;
CREATE PROCEDURE spNew(IN P_Param INT)
BEGIN
UPDATE Table
SET Field = 'some value'
WHERE ID = P_Param
END;