MySQL Forums
Forum List  »  Stored Procedures

Insert dynamic field into variable
Posted by: John Noble
Date: August 19, 2019 10:26AM

Hi folks,

I am trying to send a parameter (which is the field name) to a sproc so I can select the value from it and store it in a variable. Here is my sproc...

CREATE DEFINER=`root`@`localhost` PROCEDURE `usp_vesselAddToSettlementCodeValue`(
IN p_pKey int(12),
IN p_settCode varchar(3),
IN p_settValue decimal(12,2)
)
BEGIN

SET @c = p_settCode; -- "s06"
SET @v = p_settValue; -- 100
SET @k = p_pKey; -- 416
SET @startValue = 0;


SET @s1 = CONCAT("SELECT ", @c, "into ", @startValue, " FROM vessels WHERE pKey = ", @k);
PREPARE stmt FROM @s1;
EXECUTE stmt;


SELECT @startValue;


END

However, I am getting error message:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0 FROM vessels WHERE pKey = 461' at line 1

Any ideas ??

Options: ReplyQuote


Subject
Views
Written By
Posted
Insert dynamic field into variable
1764
August 19, 2019 10:26AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.