mysql stored procedure updates all rows instead of 1 - mysql

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;

Related

MySql stored procedure - add SELECT and UPDATE in 1 stored procedure

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

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 ;

Trouble with calling a stored procedure within an stored procedure and setting result as a variable

I'm trying to call a stored procedure from another stored procedure and store the value in a variable. The inner stored procedure basically checks if something exists and uses a select statement to return a zero or one. I keep getting an error. In this situation, MySQL is saying "=" is not valid at this position, expecting ";"
CREATE PROCEDURE `CardNames_Add` (searchedCard VARCHAR(50))
BEGIN
DECLARE exist TINYINT;
EXECUTE exist = CardNames_CheckExist searchedCard
IF (exist = 0)
INSERT INTO card_names (name)
VALUE(searchedCard)
END
You have to rewrite you other stored procedure, that you don't need btw, to give back a result
CREATE PROCEDURE CardNames_CheckExist (IN searchedCard VARCHAR(50), OUT result TINYINT )
BEGIN
--do some stuzff
result = 1
END
CREATE PROCEDURE `CardNames_Add` (searchedCard VARCHAR(50))
BEGIN
CALL CardNames_CheckExist(searchedCard,#result);
IF (#result = 0) THEN
INSERT INTO card_names (name)
VALUES (searchedCard);
END IF;
END

How to call a stored procedure with parameters in powerbuilder12

I'm using sybase powerbilder12 IDE and mySQL.
I have a stored procedure like this:
DELIMITER //
CREATE PROCEDURE CRTempTable(IN loc_code CHAR(6))
BEGIN
create temporary table mstparameter (select * from mstparameter_consolidate where location_code = 'loc_code');
END//
DELIMITER ;
I'm calling it in the powerbuilder12 like this:
DECLARE TempTBCRCall PROCEDURE FOR TempTableCR
location_code = :gs_location_code_mstparameter ;
execute TempTBCRCall;
It gives me the error :
Stored procedure execution failure1054 SQLSTATE = S0022
[MySQL][ODBC 5.2(a) Driver][mysqld-5.5.25a]Unknown column
'location_code' in 'field list'... Error Code 0
but location_code is there in my mstparameter_consolidate table.
If I set to enter the location_code manually it works fine.
This is an example that works, I hope it helps you.
DECLARE pb_acceso_usuario PROCEDURE FOR SP_ACCESO_VALIDA_DATOS_USUARIO (:gs_cod_usuario,:ls_password);
execute pb_acceso_usuario;
if SQLCA.sqlcode = 0 then
FETCH pb_acceso_usuario INTO :ln_count,:gs_des_usuario,:ls_estado;
CLOSE pb_acceso_usuario;
end if
try putting "table-name." in front of the column-name.

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;